【问题标题】:Discord.py can't define channel [fixed]Discord.py 无法定义频道 [已修复]
【发布时间】:2021-06-08 14:47:26
【问题描述】:

我正在制作一个不和谐的机器人,它会向 .jason 文件中的指定频道发送欢迎消息。因此它可以与多个服务器兼容。但是它遇到了一个错误,我不知道如何修复它。

@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)

除此之外,我已经完成了所有工作:

channel = guildInfo[ctx.message.guild.id]

它一直给我这个错误:

    channel = guildInfo[ctx.message.guild.id]
AttributeError: module 'ctx' has no attribute 'message'

编辑: 我重新编写了代码,现在它可以工作了。

这里是:

@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")

【问题讨论】:

  • 您正在尝试访问ctx 而不将其作为on_member_ready 的参数传递。这意味着,您需要在可以从welcomeMessage 和on_member_ready 访问的地方设置频道ID

标签: python json discord.py


【解决方案1】:

on_member_join 事件不提供上下文。幸运的是,传递给on_member_join 事件的成员有一个guild 属性捕获该成员所属的公会(或在本例中,加入)。用member.guild.id 替换你的ctx.message.guild.id,你应该会很好

【讨论】:

  • 现在它正在工作,但它正在使用服务器的 id,而不是频道 id:channel = guildInfo[member.guild.id] KeyError: 735082582705700904
  • 您需要确保为机器人所在的每个服务器都定义了频道,否则您将获得KeyError
  • 我该怎么做?
  • 将channel=guildInfo[member.guild.id]-line 包裹在try ... except-block 中并在KeyError 退出(这意味着该成员加入的服务器没有定义欢迎频道,很可能不想要使用该功能)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多