2017-09-05 86 views
0

我在使用move_member()来处理python机器人时遇到了麻烦。该命令的目的是通过将用户移动到某个频道来“踢”用户,然后将其删除,以便与语音呼叫断开连接,但不需要邀请回服务器。我知道,仅仅移动用户即可达到此目的,但我希望用户能够断开连接。Discord.py与move_member()的问题

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") 

Im相当肯定,你必须获得信道ID而不是“踢”,但我不明白如何去做,因为c下面颂心不是工作,甚至当我更换 ChannelType.voicediscord.ChannelType.voice

discord.utils.get(server.channels, name='kick', type=ChannelType.voice) 
+0

只要在同一页面上,如果用户尚未进行语音聊天,则您不能***成员移动到语音聊天。不和谐的dosnt支持。 – abccd

+0

看起来好像OP想要进行语音聊天踢球而不是实际踢球。如果是这样,这将是下一个显而易见的事情,OP不会踢人不在语音聊天中的人。 – xNinjaKittyx

回答

1

delete_channel('kick')将无法​​正常工作,因为你需要在channel对象,而不是一个字符串传递。

你不需要使用discord.utils来获得你想要的频道。 create_channel返回一个通道对象,所以你应该可以使用它。

但是,您确实需要获取要踢的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) 

一个例子,我会强烈建议,因为它更符合Python开始使用重写库,这将成为未来新的库反正。

+0

对于几乎完整的答案,唯一的问题是'discord.utils.get'返回给定名称的第一个成员,可能有一个通道中的多个成员具有相同的名称(其可能性非常高,填充频道)。受害者可能不是OP实际想踢的实际成员 – abccd

+1

是的,你是对的。更好的方法是通过@引用成员或者提供用户ID。我将编辑我的答案,以@作为示例,但是由OP来决定他希望如何实现(或实现两者) – xNinjaKittyx

+0

它的工作原理以及它适用于我自己的服务器,大多数情况下<15人独特的绰号。我选择了第三个,它按预期工作。但是,我只知道合理数量的python,并且API对我来说很难阅读。我可以阅读任何页面了解如何使用这样的代码? – InsertPotatoHere

0

按照Discord documentation,调用delete_channel预计Channel类型的参数。

有关Channel类的更多信息,请参阅the Channel documentation

如果我理解你正在试图做的事情,为您的代码按预期运行,你会需要保持在通道参考您正在使用的临时通道,那么你的问题的行更改为:

await bot.delete_channel(tmp_channel)