【问题标题】:How do I set the channel_ id to a function in discord.py如何将 channel_id 设置为 discord.py 中的函数
【发布时间】:2021-10-23 07:44:12
【问题描述】:
def get_channel_id(client, message):
    with open('./json/welcome.json', 'r') as f:
    channel_id = json.load(f)
    
    return channel_id[str(message.guild.id)]


@client.event
async def on_member_join(member):
    embed = discord.Embed(colour=0x767429, description=f"Welcome to my server! You are the {len(list(member.guild.members))}th member ")

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

    channel = client.get_channel(id=get_channel_id) 

    await channel.send(embed=embed)


@client.command()
async def welcomechannel(ctx, channel_id):
    with open('./json/welcome.json', 'r') as f:
        id = json.load(f)

    id[str(ctx.guild.id)] = channel_id

    with open('./json/welcome.json', 'w') as f:
        json.dump(id, f, indent=4)

    await ctx.send(f'Welcome channel changed to {channel_id}')

我想让get_channel_idfunction 成为channel = client.get_channel(id=get_channel_id) 的ID,但每次我运行它都会收到错误AttributeError: 'NoneType' object has no attribute 'send'。有人可以帮我吗

【问题讨论】:

  • 会员加入后需要打开JSON,然后从JSON中取出频道。

标签: python discord.py


【解决方案1】:

您可以大大简化代码。正如我在评论中提到的,当新用户进入服务器时,您还需要打开 JSON。

上面你自己的函数是完全多余的,不需要。

我也曾经稍微改变了你的welcomechannel 命令,因为你对channel_id 的作用不大,你可以改用channel: discord.TextChannel,结果是一样的。

这里是现在的命令:

@client.command()
async def welcomechannel(ctx, channel: discord.TextChannel): # We use discord.TextChannel
    with open('./json/welcome.json', 'r') as f:
        id = json.load(f)

    id[str(ctx.guild.id)] = channel.id # Saving JUST the ID, you also had <#ID> in it

    with open('./json/welcome.json', 'w') as f:
        json.dump(id, f, indent=4)

    await ctx.send(f'Welcome channel changed to `{channel}`')

现在检查频道。为此,我们以与使用命令相同的方式打开 JSON:

@client.event
async def on_member_join(member):
    with open('./json/welcome.json', 'r') as f:
        wchannel = json.load(f) # Define our channel
    try:
        if wchannel: # If a channel is set for the server
            wchannelsend = member.guild.get_channel(wchannel[str(member.guild.id)]) # Get the channel from the JSON
            embed = discord.Embed(color=0x767429,
                          description=f"Welcome to my server! You are the {len(list(member.guild.members))}th member ")
            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 wchannelsend.send(embed=embed) # Send the embed to the channel
        else:
            return # If no channel was set, nothing will happen
    except:
        return

【讨论】:

    猜你喜欢
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    相关资源
    最近更新 更多