我如何让我的 Python 3.6.1 Discord 机器人在服务器中创建一个新的文本通道?
How would I make my Python 3.6.1 Discord bot create a new text channel in a server?
我一直在阅读文档。文档显示了这个例子:
channel = await guild.create_text_channel('cool-channel')
我需要弄清楚如何处理 guild
,这样就不会有关于 guild
的 NameError
。 (我必须导入 guild
吗?等等)
文档:
如果您使用的是重写分支,要创建一个文本频道,您需要执行
guild = ctx.message.guild
await guild.create_text_channel('cool-channel')
如果您使用的是不受支持的异步分支,要创建文本通道,您需要执行
server = ctx.message.server
await client.create_channel(server, 'cool-channel', type=discord.ChannelType.text)
如果您需要弄清楚您使用的是哪个分支,可以print(discord.__version__)
。如果版本是 0.16.2 或更低,那么它是异步的。如果是1.0.0a,那么就是rewrite
我的命令是这样的,不知道你是否可以使用它,但它对我来说很好用所以:
@client.command()
async def create(ctx, *, name=None):
guild = ctx.message.guild
if name == None:
await ctx.send('Sorry, but you have to insert a name. Try again, but do it like this: `>create [channel name]`')
else:
await guild.create_text_channel(name)
await ctx.send(f"Created a channel named {name}")
我一直在阅读文档。文档显示了这个例子:
channel = await guild.create_text_channel('cool-channel')
我需要弄清楚如何处理 guild
,这样就不会有关于 guild
的 NameError
。 (我必须导入 guild
吗?等等)
文档:
如果您使用的是重写分支,要创建一个文本频道,您需要执行
guild = ctx.message.guild
await guild.create_text_channel('cool-channel')
如果您使用的是不受支持的异步分支,要创建文本通道,您需要执行
server = ctx.message.server
await client.create_channel(server, 'cool-channel', type=discord.ChannelType.text)
如果您需要弄清楚您使用的是哪个分支,可以print(discord.__version__)
。如果版本是 0.16.2 或更低,那么它是异步的。如果是1.0.0a,那么就是rewrite
我的命令是这样的,不知道你是否可以使用它,但它对我来说很好用所以:
@client.command()
async def create(ctx, *, name=None):
guild = ctx.message.guild
if name == None:
await ctx.send('Sorry, but you have to insert a name. Try again, but do it like this: `>create [channel name]`')
else:
await guild.create_text_channel(name)
await ctx.send(f"Created a channel named {name}")