Discord.py Ban 命令不起作用

时间:2021-01-12 07:33:04

标签: python discord.py discord.py-rewrite

大家好,我正在用 python 编写一个不和谐的机器人,我有禁止和踢命令。那么主要的问题是这些命令不起作用!我不知道为什么!请帮忙,这是我的代码:

@client.command(aliases=['Kick','KICK','KİCK'])
@commands.has_permissions(kick_members=True)
async def kick(ctx, member : discord.Member, *, reason=None):
    await member.kick(reason=reason)
    await ctx.send(f"{member.mention} is kicked.")

@kick.error
async def kick_error(ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("Sorry, but if you want to use this command you need to have **Kick Members** permission.")

@client.command(aliases=['Ban','BAN'])
@commands.has_permissions(ban_members=True)
async def ban(ctx, member : discord.Member, *, reason=None):
    await member.ban(reason=reason)
    await ctx.send(f'{member.mention} is banned.')

#ban someone in a server error
@ban.error
async def ban_error(ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("Sorry, but if you want to use this command you need to have **Ban Members** permission.")

@client.command(aliases=['Unban','UNBAN'])
async def unban(ctx, *, member):
    banned_users = await ctx.guild.bans()
    member_name, member_discriminator = member.split('#')

    for ban_entry in banned_users:
        user = ban_entry.user

        if (user.name, user.discriminator) == (member_name, member_discriminator):
            await ctx.guild.unban(user)
            await ctx.send(f'{user.mention} is unbanned.')
            return

#unban someone in a server error
@unban.error
async def unban_error(ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("Sorry, but if you want to use this command you need to have **Ban Members** permission.

2 个答案:

答案 0 :(得分:0)

编辑:不要忘记导入以下内容:

from flask import Flask, render_template, Response, request
import spotipy
from spotipy.oauth2 import SpotifyOAuth

application = Flask(__name__)

auth = SpotifyOAuth("xxxxxxxx",
                    "xxxxxxxx", "http://mywebsite.com/callback", cache_path=".spotifycache", scope="user-library-read,playlist-modify-public")
sp = spotipy.Spotify(auth_manager=auth)
user = sp.current_user()

@application.route('/')
def index():
  return render_template('index.html')

@application.route('/about.html')
def about():
  return render_template('about.html')

if __name__ == '__main__':
  application.run(debug=True)

import discord
import os
from discord.ext import commands,tasks
from discord.ext.commands import has_permissions, CheckFailure

上面的命令是对你的禁令命令的重做。我使用了 @commands.command() @commands.has_permissions(ban_members=True) async def bAn(ctx,*, member : discord.Member = None, reason=None): if member is None: await ctx.send("Please mention someone to ban") if reason is None: reason = "Reason was not specified" await ctx.send(f'{member.mention} is banned.') await member.ban(reason=reason) # could use ctx.guild.ban(member, reason=reason) here, works the same way though.

而不是 client.commands()

并声明 Member 为 None 并指定一个条件,如果未提及任何成员,bot 将要求用户提及用户 如果未提及原因,则默认原因为“未指定原因” kick 命令遵循与 ban 相同的模式。

此外,对于取消禁令,您可以使用以下内容

commands.command()

注意:命令“ben”是ban,我改为“ben”,因为我在另一个文件中已经有了一个ban命令!

enter image description here

unban output

答案 1 :(得分:-1)

我认为您在参数成员中有:discord.Member,但不和谐提供了成员提及。至少我得到了 member.mention,然后遍历公会中的成员列表,将 member.mention 与提供的 member.mention 进行比较,然后我就有了该成员,我可以禁止该成员。希望你理解