I'm trying to setup a welcome message using Discord.py, and this is what I currently have:
hydrabot/hydra/settings.py
import discord
# Welcomer settings
welcomeChannel = "819624923445985310"
# Bot intents settings
intents = discord.Intents(members=True)
# Command prefix
command_prefix = '$'
# Bot help page
description = f'''Hydra Commands
Hydra's command prefix is "{command_prefix}"
'''
hydrabot/hydra/main.py
import os
import discord
from discord.ext import commands
from settings import(
intents,
command_prefix,
description)
from welcome_message import welcomeMessage
bot = commands.Bot(command_prefix=command_prefix, description=description, intents=intents)
@bot.event
async def on_member_join(member):
print(f"{member.name} has joined the server. Attempting to send welcome message...")
await member.send(welcomeMessage)
print(f'Sent welcome message to {member.name}.')
hydrabot/hydra/welcome_message.py
import discord
welcomeMessage = f'Welcome to the server, {member.mention}!'
Whenever I run the program, I get this error:
Traceback (most recent call last):
File "C:\Users\liljo\OneDrive\Projects\hydrabot\hydra\main.py", line 12, in <module>
from welcome_message import welcomeMessage
File "C:\Users\liljo\OneDrive\Projects\hydrabot\hydra\welcome_message.py", line 3, in <module>
welcomeMessage = f'Welcome to the server, {member.mention}!'
NameError: name 'member' is not defined
Wasn't member
defined when I imported discord
? Here is what happens if I use from discord import member
instead of import discord
: hydrabot/hydra/welcome_message.py
Traceback (most recent call last):
File "C:\Users\liljo\OneDrive\Projects\hydrabot\hydra\main.py", line 12, in <module>
from welcome_message import welcomeMessage
File "C:\Users\liljo\OneDrive\Projects\hydrabot\hydra\welcome_message.py", line 3, in <module>
welcomeMessage = f'Welcome to the server, {member.mention}!'
AttributeError: module 'discord.member' has no attribute 'mention'
How is there not an attribute named mention
, but the program runs fine whenever I move the welcome message back to main.py
?
You are not giving the member
variable from main.py
to welcome_message.py
.
Solution: You can just directly define the welcome message in main.py, it isn't worth it to define it in another file
Solution: You can put the welcome message in welcome_message.py in a function where you pass the parameter member
welcome_message.py:
def welcomemessage(member):
...
return welcomeMessage
main.py:
import welcome_message
welcome_message.welcomemessage(member)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.