I'm trying to make my code more efficient for my discord bot by using the from_dict method for my help command embedded messages, but it's not wanting to let me use the message variable because it hasn't been declared yet in the dictionary, even though the command function takes in the variable that would be used for the dictionary that holds it.
Here's my code to hopefully try to explain it better than I tried,
message = discord.Message
helpDict = {'footer': {'text': f'Requested by: {message.author.display_name}', 'icon_url': f'{message.author.avatar_url}'},
'author': {'name': 'Anuva Seshat, the Book Keeper', 'icon_url': 'https://i.postimg.cc/FRfS9pw4/Magi-Matured.jpg'},
'fields': [],
'color': 15844367, 'type': 'rich', 'description': '', 'title': ''
}
# Custom Help Commands
@bot.group(invoke_without_command = True)
async def help(message):
tempDict = helpDict
tempDict['title'] = f'Help, provided by {bot.user}'
tempDict['description'] = '$help lists all commands in their respective categories.'
# tempDict['footer']['text'] = f'Requested by: {message.author.display_name}'
# tempDict['footer']['icon_url'] = f'{message.author.avatar_url}'
tempDict['fields'].append({'inline': False, 'name': 'DM Commands', 'value': 'Commands for the DM role only: $addLesson, $addPainting, $answers, $grade, $newNPC, $play, $readmit, $removeLesson'})
tempDict['fields'].append({'inline': False, 'name': 'DM & Student Commands', 'value': 'Commands for both Student and DM roles: $grades, $showNPC'})
tempDict['fields'].append({'inline': False, 'name': 'Fun Commands', 'value': 'Commands of fun for all: $guessingGame, $hello, $painting, $roll'})
tempDict['fields'].append({'inline': False, 'name': 'Student Commands', 'value': 'Commands for the Student role only: $enlist, $lesson, $quiz'})
tempDict['fields'].append({'inline': False, 'name': 'Testing Commands', 'value': 'Commands for the DM role to test the bot: $initDB, $init_testNPC, $showDB, $showNpcList, $showTestNPC, $testEmbed, $testPicDB'})
tempDict['fields'].append({'inline': False, 'name': 'Message Deletion', 'value': 'For help message deletion, please react to the message.'})
em = discord.Embed.from_dict(tempDict)
await message.send(embed = em)
await message.message.delete()
CodePudding user response:
In the helpDict array you are using the message variable which SENDS the message not gets it.
You need to use the "{message.author.display_name}" inside of the command because that's the only place it gets the message author from.
The message variable your needing to get is the "async def help(message)" one.
.
.
NOT THIS IGNORE EDITED: Could it be because your dict is called "helpDict" and the 'em' variable is trying to get "tempDict"?
CodePudding user response:
The question is a little unclear but I'll try to cover a few things that you might have missed.
When you do tempDict = helpDict
you are creating a reference. Feel free to view a guide on using copy
import copy
def just_assignment(d1):
print("just_assignment")
d2 = d1
d2['edited'] = "new"
print(d1 == d2)
def using_copy(d1):
print("using copy.copy")
d2 = copy.copy(d1)
d2['edited'] = "new"
print(d1 == d2)
def using_deep_copy(d1):
print("using copy.deepcopy")
d2 = copy.deepcopy(d1)
d2['edited'] = "new"
print(d1 == d2)
just_assignment({"foo" : "bar"})
# just_assignment
# True
using_copy({"foo" : "bar"})
# using copy.copy
# False
using_deep_copy({"foo" : "bar"})
# using copy.deepcopy
# False
Given you are creating a placeholder embed via helpDict
, why not just add the fields when they are present?
helpDict = {
'author': {'name': 'Anuva Seshat, the Book Keeper', 'icon_url': 'https://i.postimg.cc/FRfS9pw4/Magi-Matured.jpg'},
'fields': [],
'color': 15844367, 'type': 'rich', 'description': '', 'title': ''
}
# Custom Help Commands
@bot.group(invoke_without_command = True)
async def help(message):
tempDict = copy.copy(helpDict)
tempDict.update({
'footer': {
'text': f'Requested by: {message.author.display_name}',
'icon_url': f'{message.author.avatar_url}',
}
})
Hopefully this helps!