[英]Discord.py can't define channel [fixed]
I'm making a discord bot that sends a welcome message to a specified channel in the.jason file.我正在制作一个 discord 机器人,它向 .jason 文件中的指定频道发送欢迎消息。 So it could be compatible with multiple servers.因此它可以与多个服务器兼容。 But it runs into an error and I can't figure out how to fix it.但它遇到了一个错误,我不知道如何修复它。
@client.command()
@commands.has_permissions(administrator=True)
async def welcomeMessage(ctx):
with open("guild.json", "r") as f:
guildInfo = json.load(f)
guildInfo[ctx.message.guild.id] = ctx.message.channel.id #sets the channel it was sent to as the default welcome message channel
with open("guild.json", "w") as f:
json.dump(guildInfo, f)
@client.event
async def on_member_join(member):
with open("guild.json", "r") as f:
guildInfo = json.load(f)
channel = guildInfo[ctx.message.guild.id] #this keeps causing the error
embed = discord.Embed(colour=0x07faf6, description=f"***Welcome to the Server***")
embed.set_thumbnail(url=f"{member.avatar_url}")
embed.set_author(name=f"{member.name}", icon_url=f"{member.avatar_url}")
embed.set_footer(text=f"{member.guild}", icon_url=f"{member.guild.icon_url}")
embed.timestamp = datetime.datetime.utcnow()
await channel.send(embed=embed)
I have got everything working except this:除了这个,我已经完成了所有工作:
channel = guildInfo[ctx.message.guild.id]
It keeps giving me this error:它一直给我这个错误:
channel = guildInfo[ctx.message.guild.id]
AttributeError: module 'ctx' has no attribute 'message'
EDIT: I redid the code and now it works.编辑:我重新编写了代码,现在它可以工作了。
here it is:这里是:
@client.command(pass_context=True)
@commands.has_permissions(administrator=True)
async def welcomechannel(ctx):
fileopen = str(ctx.message.guild.id) + ".json"
file = open(fileopen, "w")
data = {}
data["channel"] = ctx.message.channel.id
json.dump(data, file)
print("The channel has been added to the list")
@client.event
async def on_member_join(member):
colours = [0x3348FF, 0x000000, 0xFFFFFF]
try:
fileopen = str(member.guild.id) + ".json"
file = open(fileopen, "r")
data = json.load(file)
channelid = data["channel"]
channel = client.get_channel(id=channelid)
embed = discord.Embed(colour=random.choice(colours), description="***Welcome***")
embed.set_thumbnail(url=f"{member.avatar_url}")
embed.set_author(name=f"{member.name}", icon_url=f"{member.avatar_url}")
embed.set_footer(text=f"{member.guild}", icon_url=f"{member.guild.icon_url}")
embed.timestamp = datetime.datetime.utcnow()
await channel.send(embed=embed)
except:
print("The server doesn't have a welcome channel")
The on_member_join
event does not provide a context. on_member_join
事件不提供上下文。 Luckily, the member passed to the on_member_join
event has a guild
attribute capturing the guild the member belongs to (or in this case, joined).幸运的是,传递给on_member_join
事件的成员有一个guild
属性,该属性捕获该成员所属的公会(或在本例中为加入的)。 Replace your ctx.message.guild.id
by member.guild.id
and you should be good用member.guild.id
ctx.message.guild.id
应该很好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.