簡體   English   中英

discord.py ban 命令不執行任何操作

[英]discord.py ban command does not do anything

似乎禁令命令在這里不起作用。 “你好”和“侮辱我”響應有效,它在運行時打印出機器人名稱和服務器名稱。 完整的 main.py:

import os
import random
import discord
from dotenv import load_dotenv
from discord.ext import commands

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')
PREFIX = os.getenv('DISCORD_PREFIX')

client = discord.Client()
bot = commands.Bot(command_prefix='!',
                    help_command=None) # help_command to disable the default one created by this library.




@client.event
async def on_ready():
    for guild in client.guilds:
        if guild.name == GUILD:
            break
    print(
        f'{client.user} is connected to the following guild:\n'
        f'{guild.name}(id: {guild.id})'
    )


@client.event
async def on_member_join(member):
    await member.create_dm()
    await member.dm_channel.send(
        f'Hi {member.name}, welcome to my Discord server!'
    )



@client.event
async def on_message(message):
    reponse = ''
    if message.author == client.user:
        return
    greeting = [
        f"Hello there, {message.author.mention}",
        f"hi!!",
        'Penis enlargement pills are on their way!',
        f"You're stupid, {message.author.mention}",
    ]
    insult = [
        "You're a dumb dumb!",
        "You stupid meanie face!",
        "Go eat a dick, you stupid mudblood!"
    ]
    if "hello" in message.content:
        response = random.choice(greeting)
        await message.channel.send(response)
    if 'insult me' in message.content:
        response = random.choice(insult)
        await message.channel.send(response)


#bans a user with a reason
@bot.command()
@commands.has_any_role("Keyblade Master","Foretellers")
async def ban (ctx, member:discord.User=None, reason =None):
    if member == None or member == ctx.message.author:
        await ctx.channel.send("You cannot ban yourself")
        return
    if reason == None:
        reason = "For being a jerk!"
    message = f"You have been banned from {ctx.guild.name} for {reason}"
    await member.send(message)
    # await ctx.guild.ban(member, reason=reason)
    await ctx.channel.send(f"{member} is banned!")


client.run(TOKEN)

我認為最相關的部分是:

client = discord.Client()
bot = commands.Bot(command_prefix='$',
                    help_command=None) # help_command to disable the default one created by this library.

#bans a user with a reason
@bot.command()
async def ban (ctx, member:discord.User=None, reason =None):
    if member == None or member == ctx.message.author:
        await ctx.channel.send("You cannot ban yourself")
        return
    if reason == None:
        reason = "For being a jerk!"
    message = f"You have been banned from {ctx.guild.name} for {reason}"
    await member.send(message)
    # await ctx.guild.ban(member, reason=reason)
    await ctx.channel.send(f"{member} is banned!")

我確定我只是在某處的代碼中錯誤地實現了機器人/命令,但我不確定在哪里。 當我嘗試運行機器人或在聊天中鍵入命令時,我根本沒有收到任何錯誤。

您制作了一個clientbot ,但您只將client用於您的代碼。 bot中注冊的命令永遠不會被調用,因為它甚至沒有運行。

您應該查看教程並在discord.py Discord 服務器中尋求幫助

我不確定,但如果沒有輸入原因並且原因 = 原因更好,您需要返回原因。 所以你的代碼應該是這樣的:

async def ban (ctx, member:discord.User=None, reason = reason):
    if member == None or member == ctx.message.author:
        await ctx.channel.send("You cannot ban yourself")
        return
    if reason == None:
        reason = "For being a jerk!"
        return
    message = f"You have been banned from {ctx.guild.name} for {reason}"
    await member.send(message)
    # await ctx.guild.ban(member, reason=reason)
    await ctx.channel.send(f"{member} is banned!")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM