Reputation: 103
What my program should do is in a discord server the user types ~modmail. This message then arrives in their dm:
The user should then reply in the dm with the question specified. All of this information is then sent to a specifically name channel in the original server.
Now here is the problem: Global variables aren't be able to be used in the new on_message function:
name = "reports-log"
channel = get(message.server.channels, name=name, type=discord.ChannelType.text)
original = message.author
dm_embed=discord.Embed(title="Modmail/Support:", color=discord.Color.green(), description=" ")
dm_embed.add_field(name="Please declare your problem/question and send it in this channel!", value="**Question must be on one message**\n*You can make a new line if you press Shift + Enter*")
dm_embed.set_footer(text="Valid for 1 minute")
await client.send_message(message.author, embed=dm_embed)
@client.event
async def on_message(message):
if message.server is None and message.author != client.user:
global channel
global original
question = message.content
report_embed = discord.Embed(title="New Modmail:" , color=discord.Color.green())
report_embed.add_field(name="User: ", value=message.author.mention)
report_embed.add_field(name="Question: ", value=question)
await client.send_message(channel, embed=report_embed)
await client.send_message(original, embed=discord.Embed(color=discord.Color.green(), description="Your support request has been recieved, you will recieve help shortly."))
Not sure why these variables aren't able to be used in my function. Hope someone has a solution for me. Thanks.
Upvotes: 2
Views: 7180
Reputation: 3205
Instead of trying to use global variables for a function that requires contextual isolation, you should try and keep things in the same run.
If a second user tries to send a modmail while a first user is in the process, you are going to end up overwriting your original
global with the second user.
As mentioned in the comments to your question, your problem can be solved by using client.wait_for_message
instead of globals.
The second issue is that you are attempting to define discord variables in the global scope by searching the client's caches, which don't get populated until after you call client.run()
or your preferred starting mechanism.
# You can still store a global channel, but must find it after you start
@client.event
async def on_ready():
# This is only called once the client has downloaded all its data.
global channel
channel = get(...)
def message_mods(message):
request = await client.send_message(message.author, ....)
response = await client.wait_for_message(60,
author = message.author,
channel = request.channel
)
# Generate report embed using the `response` object
e = discord.Embed(title="New modmail.", colour=discord.Colour.green(),
description=response.content)
e.set_author(name=response.author.name, icon_url=response.author.avatar_url)
# Use the channel found in on_message, no need for global
# because we didn't redefine channel at all.
client.send_message(channel, embed=e)
# Reply to the user
client.send_message(response.channel, ...)
Upvotes: 1