我想让每个服务器通道



我所说的"每台服务器"是指通道绑定到一台服务器下面是我编写的代码:

def get_channel(client,message):
with open('channels.json', 'r') as f:
channels = json.load(f)
return channels[str(message.guild.id)]
@client.event
async def on_guild_join(guild):
with open('channels.json', 'r') as f:
channels = json.load(f)
channels[str(guild.id)] = ''
with open('channels.json', 'w') as f:
json.dump(channels,f)
@client.command()
async def cc(ctx, prefix):
guild = ctx.guild
with open('channels.json', 'r') as f:
channels = json.load(f)
channels[str(guild.id)] = prefix
with open('channels.json', 'w') as f:
json.dump(channels,f)
await ctx.send('done')

这是我将函数放入

的代码
@client.event
async def on_member_join(member):
channel = discord.utils.get(member.guild.channels, name=get_channel)
await channel.send(f'Welcome {member.mention}')

数据库中的文本是正确的我得到的错误:等待通道。发送(f 'Welcome {member.mention}")AttributeError: 'NoneType'对象没有属性'send'

def get_channel(guild):
with open('channels.json', 'r') as f:
channels = json.load(f)
return channels[str(guild.id)]
@client.event
async def on_guild_join(guild):
with open('channels.json', 'r') as f:
channels = json.load(f)
channels[str(guild.id)] = ''
with open('channels.json', 'w') as f:
json.dump(channels,f)
@client.command()
async def channel_define(ctx, channel_name):
guild = ctx.guild
with open('channels.json', 'r') as f:
channels = json.load(f)
channels[str(guild.id)] = channel_name
with open('channels.json', 'w') as f:
json.dump(channels,f)
await ctx.send('done')
@client.event
async def on_member_join(member):
channel = discord.utils.get(member.guild.channels, name=get_channel(member.guild))
await channel.send(f'Welcome {member.mention}')

这是代码的更正版本。但是我建议你注册通道id而不是名称。

如果您想使用带有id的get方法,请参阅文档:https://discordpy.readthedocs.io/en/stable/api.html#discord.utils.get

channel = discord.utils.get(member.guild.channels, name='get_channel')

在这一行中,您可以在成员加入的公会通道中搜索一个名为"get_channel"的通道。但是get函数没有找到,所以它返回None。此通道不存在,或者此公会的缓存未加载。

加载公会缓存:

await guild.chunk()

相关内容

  • 没有找到相关文章

最新更新