如何使用 discord.py API 将用户移动到 discord 上的特定频道

2022-01-15 00:00:00 python discord discord.py

问题描述

好的,所以我知道执行此操作的命令,但我的问题是我不知道要传递给参数的参数.我希望我的代码获取用户的消息内容,然后将用户移动到名为afk"的语音频道.这是我的代码片段:

Okay, so I know the command to do this but my issue is I do not know what arguments to pass to the parameters. I want my code to take a user's message content and then move the user to a voice channel named "afk". Here is a snippet of my code:

我要做的就是将输入单词 !move 的用户移动到另一个语音频道.如果我的代码不好,我很抱歉,但我只是需要这个.

All I want to do is move a user that types the words !move in any case to be moved to another voice channel. I am sorry if my code is bad but I just need this down.

我知道您可能需要查看我的定义,但仅此而已:

I know you might need to see my definitions but all it is:

def on_message(message):
    if '!MOVE' in message.content.upper():
        author = message.author
        voice_channel = id('afk')
        await client.move_member(author, voice_channel)


解决方案

client.move_member 有两个参数:一个 Member 和一个 Channel.我们可以使用 discord.utils.find 从服务器频道列表中获取频道.

client.move_member takes two arguments: a Member and a Channel. We can use discord.utils.find to get the channel from the servers list of channels.

channel = discord.utils.find(lambda x: x.name == 'afk', message.server.channels)
await client.move_member(message.author, channel)

一些进一步的说明:

  • 对于 afk 频道,上述内容实际上是不必要的,因为服务器有一个 Server.afk_channel 属性.
  • 您还应该使用 discord.ext.commands 扩展来实现您的命令,以防止您的 on_message 变得混乱.
  • The above is actually unnecessary for the afk channel, as servers have a Server.afk_channel attribute.
  • You should also be using the discord.ext.commands extension to implement your commands, to keep your on_message from getting cluttered.

相关文章