python - 在 discord.py 中无法获得语音频道
问题描述
import os
from discord.ext import commands
bot = commands.Bot(command_prefix='~')
for fileName in os.listdir('cogs'):
if fileName.endswith('.py') and fileName != '__init__.py':
bot.load_extension(f'cogs.{fileName[:-3]}')
bot.run(* TOKEN * )
问题出在下面的代码上:
import discord
from discord.ext import commands
class MoveMembers(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.channel_to_move= discord.Client().get_channel(* channel ID *)
@commands.command()
async def send_to_channel(self, ctx, *members: discord.Member):
for member in members:
if member.voice:
print(type(self.channel_to_move))
await member.move_to(self.channel_to_move)
await ctx.send('message recieved')
def setup(bot):
bot.add_cog(MoveMembers(bot))
此功能的重点是移动一个或多个已被引用到特定语音通道的成员。
例如:“~send_to_channel @person”
该函数被调用,但由于某种原因“discord.Client().get_channel(* channel ID *)”不返回语音通道或任何通道。我已经阅读了堆栈溢出中的文档和其他一些答案,但我不知道出了什么问题......
**** 编辑 **** 还有人知道为什么这段代码不起作用:
def move_rights():
def predicate(ctx):
return commands.check_any(commands.is_owner(),
commands.has_role("Move Rights"))
return commands.check(predicate)
@commands.command()
@move_rights()
async def send_to_gulag(self, ctx, *members: discord.Member):
for member in members:
if member.voice:
await member.move_to(self.gulag)
await ctx.send('message recieved')
每个人都可以使用这个命令,所以我不知道“move_rights()”函数是否总是返回“true”或者只是没有正确实现它。(代码是原始问题中代码的一部分)
解决方案
在self.channel_to_move= discord.Client().get_channel(* channel ID *)
你不应该引用类本身,而是引用实例
self.channel_to_move = self.bot.get_channel(* channel ID *)
了解类和实例之间的区别很重要
推荐阅读
- awk - 如何拒绝以awk中的某些文本开头的字符串?
- regex - regexp 语句中没有前面的字符
- mysql - 如何从 MySQL 查询中的电子邮件地址中删除“加号”标签?
- react-native - 收到通知后从后台启动 React Native App
- csv - 使用命令行复制 csv 的第一列 [已编辑]
- python - 如何使用 selenium 访问嵌套的 html 元素?
- javascript - 我尝试制作一个机器人,但是当我运行 kick 命令时发生了
- python - 为 3D 两轴散点图制作 Matplotlib 图例
- python - 用于切片数组的 Python IndexError
- scala - 具有复杂多态性界限的 Scala 语法