Discord机器人未创建频道

时间:2020-04-15 18:14:31

标签: python discord.py

我写了一个不和谐的机器人,该机器人应该创建一个语音通道并将我移到创建的通道。没有为我的代码创建通道,我想问一下如何解决它。如果可能的话,我也很乐意接受有关移动玩家的建议。这是我的代码:

import discord

class MyClient(discord.Client):
    async def on_ready(self):
        print("Login successful")
        print("launching on version " + discord.__version__)


    async def create(self, ctx, message, guild):
        if message.author == client.user:
            return
        if message.content.startswith("!create"):
            server = ctx.message.server
            channelname = "test-channel"
            await discord.create_channel(server, channelname, type=discord.ChannelType.voice)
            print("Channel " + channelname + "created!")


client = MyClient()
client.run("TOKEN")

1 个答案:

答案 0 :(得分:0)

使用system of events确定要执行的代码。如果要运行代码,则需要将其与事件关联。您可能想通过为类编写一个message方法来使用on_message事件。该框架根据方法的名称确定要执行的回调,因此,on_message事件触发时,将运行message方法:

class MyClient(discord.Client):
    async def on_ready(self):
        print("Login successful")
        print("launching on version " + discord.__version__)


    async def on_message(self, message):
        if message.author == client.user:
            return
        if message.content.startswith("!create"):
            server = ctx.message.server
            channelname = "test-channel"
            await server.create_voice_channel(channelname)
            print("Channel " + channelname + "created!")