问题描述
感谢之前帮助我解决最后一个问题的人 我试图做的是为一个函数创建一个不同的文件,当你输入“!创建”+(我希望频道的名称)它会创建一个具有指定名称的新频道,我希望该文件能够在主频道回调
代码 1:
import os
import discord
import createchannel
client = discord.Client()
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
commands = {
'$Hello' : 'Hello! write "$Help" to find more about the CazB0T ','$Help' : 'Hi again! The CazB0T is still in beta stages...the only 5 commands are $Hello,$Help,$Test1,$Test2,and $Test3','$Test1' : 'This is Test1','$Test2' : 'This is Test2','$Test3' : 'This is Test3'
}
createchannel()
my_secret = os.environ['token']
@client.event
async def on_message(message):
if message.author == client.user:
return
for key,value in commands.items():
if message.content.startswith(key):
await message.channel.send(value)
client.run(os.getenv('token'))
代码 2:
import discord
import os
client = discord.Client()
@client.event
async def on_chancreate(channel):
channelname = input()
if channel.content == "!create" + channelname:
channel = await channel.create_text_channel(channelname)
client.run(os.getenv('token'))
解决方法
根据评论,我假设您想通过命令创建频道。为此,您需要考虑以下几点:
如果您想使用 commands
,您必须将 client = discord.Client()
更改为 client = commands.Bot(command_prefix="YourPrefix")
并从 commands
导入 discord.ext
:
from discord.ext import commands
client = commands.Bot(command_prefix="!") # Example prefix
首先:您必须设置必须满足的条件。您希望能够为频道指定自己的名称吗?假设这是必需的参数:
@client.command()
async def cchannel(ctx,*,name):
- 我们说过
name
是必需参数。
使用 -
*
以便我们还可以输入名称,如t e s t
而不仅仅是test
。
第二:您需要定义要在其中创建频道的公会。为此,我们使用:
guild = ctx.message.guild
最后我们要create a text channel。 为此,我们使用:
@client.command()
async def channel(ctx,name):
guild = ctx.message.guild # Get the guild
await guild.create_text_channel(name=name) # Create a text channel based on the input
await ctx.send(f"Created a new channel with the name `{name}`") # Optional
- 在创建频道时,我们说名称应该是我们必需的参数
name
。