【发布时间】:2021-06-10 18:22:27
【问题描述】:
我正在尝试使用 Discord.py 设置欢迎消息,这是我目前拥有的:
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}!'
每当我运行程序时,我都会收到此错误:
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
我导入 discord 时没有定义 member 吗?如果我使用from discord import member 而不是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'
为什么没有一个名为mention的属性,但是每当我将欢迎消息移回main.py时程序运行良好?
【问题讨论】:
-
错误说明一切,
member未定义。你可以尝试使用member = discord.Member -
您没有从 discord.py 开始所需的最少知识。了解更多 OOP 和一般编程,这对您来说会容易得多。 discord.py 不是为初学者准备的库
标签: python discord discord.py bots