Discord.py move_member() 有问题
Discord.py trouble with move_member()
我在为 python 机器人使用 move_member()
时遇到问题。该命令的目的是 "kick" 一个用户,将他们移动到一个频道,然后删除它,这样他们就可以断开语音通话,但不需要邀请回到服务器。我知道只需移动用户即可实现此目的,但我希望用户断开连接。
import discord
import random
import time
import asyncio
from discord.ext import commands
from discord.ext.commands import Bot
bot = commands.Bot(command_prefix="!")
@bot.event
async def on_ready():
await bot.change_presence(game=discord.Game(name='with fire'))
print("Logged in as " + bot.user.name)
print(discord.Server.name)
@bot.command(pass_context=True)
async def kick(ctx,victim):
await bot.create_channel(message.server, "kick", type=discord.ChannelType.voice)
await bot.move_member(victim,"kick")
await bot.delete_channel("bad boi")
bot.run('TOKEN_ID')
该行给出了错误:
The channel provided must be a voice channel
await bot.move_member(victim,"kick")
这一行给出了这个错误:
'str' object has no attribute 'id'
await bot.delete_channel("kick")
我很确定您必须获取频道 ID 而不是 "kick",但我不知道具体如何操作,因为下面的代码无法正常工作,即使我替换了
ChannelType.voice
到 discord.ChannelType.voice
discord.utils.get(server.channels, name='kick', type=ChannelType.voice)
根据 Discord documentation,对 delete_channel
的调用需要类型为 Channel
的参数。
有关 Channel
class 的更多信息,请参阅 the Channel documentation
如果我理解您要执行的操作,为了让您的代码按预期 运行,您将需要维护对频道的引用用作您的临时频道,然后将您的违规行更改为:
await bot.delete_channel(tmp_channel)
delete_channel('kick')
将不起作用,因为您需要传入 channel
对象而不是字符串。
您不需要使用 discord.utils 来获取您想要的频道。 create_channel
returns 一个频道对象,所以你应该可以使用它。
但是,您确实需要获得要踢的 Member
对象。您还错误地引用了 message.server
而不是 ctx.message.server
@bot.command(pass_context=True)
async def kick(ctx, victim):
victim_member = discord.utils.get(ctx.message.server.members, name=victim)
kick_channel = await bot.create_channel(ctx.message.server, "kick", type=discord.ChannelType.voice)
await bot.move_member(victim_member, kick_channel)
await bot.delete_channel(kick_channel)
现在,如果您正在使用重写库,则必须执行以下操作
@bot.command()
async def kick(ctx, victim):
victim_member = discord.utils.get(ctx.guild.members, name=victim)
kick_channel = await ctx.guild.create_voice_channel("kick")
await victim_member.move_to(kick_channel, reason="bad boi lul")
await kick_channel.delete()
正如评论中提到的 abccd,这被评估为一个字符串,这不能保证您会踢对人。 discord.utils.get
将获取第一个结果,如果多个用户具有相同的名称,则不一定是正确的用户。
更好的方法是使用@user 或使用UserID。这是旧库中的示例
@bot.command(pass_context=True)
async def kick(ctx):
victim = ctx.message.mentions[0]
kick_channel = await bot.create_channel(ctx.message.server, "kick", type=discord.ChannelType.voice)
await bot.move_member(victim,kick_channel)
await bot.delete_channel(kick_channel)
我强烈建议开始使用重写库,因为它更像 Pythonic,而且无论如何它将成为未来的新库。
我在为 python 机器人使用 move_member()
时遇到问题。该命令的目的是 "kick" 一个用户,将他们移动到一个频道,然后删除它,这样他们就可以断开语音通话,但不需要邀请回到服务器。我知道只需移动用户即可实现此目的,但我希望用户断开连接。
import discord
import random
import time
import asyncio
from discord.ext import commands
from discord.ext.commands import Bot
bot = commands.Bot(command_prefix="!")
@bot.event
async def on_ready():
await bot.change_presence(game=discord.Game(name='with fire'))
print("Logged in as " + bot.user.name)
print(discord.Server.name)
@bot.command(pass_context=True)
async def kick(ctx,victim):
await bot.create_channel(message.server, "kick", type=discord.ChannelType.voice)
await bot.move_member(victim,"kick")
await bot.delete_channel("bad boi")
bot.run('TOKEN_ID')
该行给出了错误:
The channel provided must be a voice channel
await bot.move_member(victim,"kick")
这一行给出了这个错误:
'str' object has no attribute 'id'
await bot.delete_channel("kick")
我很确定您必须获取频道 ID 而不是 "kick",但我不知道具体如何操作,因为下面的代码无法正常工作,即使我替换了
ChannelType.voice
到 discord.ChannelType.voice
discord.utils.get(server.channels, name='kick', type=ChannelType.voice)
根据 Discord documentation,对 delete_channel
的调用需要类型为 Channel
的参数。
有关 Channel
class 的更多信息,请参阅 the Channel documentation
如果我理解您要执行的操作,为了让您的代码按预期 运行,您将需要维护对频道的引用用作您的临时频道,然后将您的违规行更改为:
await bot.delete_channel(tmp_channel)
delete_channel('kick')
将不起作用,因为您需要传入 channel
对象而不是字符串。
您不需要使用 discord.utils 来获取您想要的频道。 create_channel
returns 一个频道对象,所以你应该可以使用它。
但是,您确实需要获得要踢的 Member
对象。您还错误地引用了 message.server
而不是 ctx.message.server
@bot.command(pass_context=True)
async def kick(ctx, victim):
victim_member = discord.utils.get(ctx.message.server.members, name=victim)
kick_channel = await bot.create_channel(ctx.message.server, "kick", type=discord.ChannelType.voice)
await bot.move_member(victim_member, kick_channel)
await bot.delete_channel(kick_channel)
现在,如果您正在使用重写库,则必须执行以下操作
@bot.command()
async def kick(ctx, victim):
victim_member = discord.utils.get(ctx.guild.members, name=victim)
kick_channel = await ctx.guild.create_voice_channel("kick")
await victim_member.move_to(kick_channel, reason="bad boi lul")
await kick_channel.delete()
正如评论中提到的 abccd,这被评估为一个字符串,这不能保证您会踢对人。 discord.utils.get
将获取第一个结果,如果多个用户具有相同的名称,则不一定是正确的用户。
更好的方法是使用@user 或使用UserID。这是旧库中的示例
@bot.command(pass_context=True)
async def kick(ctx):
victim = ctx.message.mentions[0]
kick_channel = await bot.create_channel(ctx.message.server, "kick", type=discord.ChannelType.voice)
await bot.move_member(victim,kick_channel)
await bot.delete_channel(kick_channel)
我强烈建议开始使用重写库,因为它更像 Pythonic,而且无论如何它将成为未来的新库。