AutoMod导致Discord.py中出现问题

2024-09-29 19:34:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在尝试使用Discord.py编程Discord机器人。如果我注释掉程序中的“自动调节”部分,Bot就会工作。但是,当我在程序中使用“自动调节”时,bot只运行自动调节而不运行其他任何功能。有谁能帮我找到一种方法来确保程序中的所有内容都正常工作吗

这是我的密码:

#-----------------------------------Setup----------------------------------
import os
import keep_alive
import discord
from discord.ext import commands, tasks
import random
import asyncio
import time
from itertools import cycle
import aiofiles

bot = commands.Bot(command_prefix='?')
bot.remove_command("help")
bot.warnings = {} 

@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')
    await bot.change_presence(status = discord.Status.online, activity=discord.Game("Some Trashy Games! "))
  
#slowmode command
@bot.command()
@commands.has_permissions(manage_messages=True)
async def slowmode(ctx, seconds: int):
    await ctx.channel.edit(slowmode_delay=seconds)
    slowmode_embed = discord.Embed(title="Slowmode", description="A slowmode was set for this channel", colour=discord.Colour.green())
    await ctx.send(embed=slowmode_embed, delete_after=5.0)

#----------------------------------Auto Moderation-------------------------------------

#bad words
bad_words = ["fuck","nigga","fuk","cunt","nut","bitch","dick","d1ck","pussy","asshole","b1tch","b!tch","blowjob","cock","c0ck", "ass","nigger","motherfucker", "f u", "shit", "sht", "whore", "faggot", "fag"]

@bot.event
async def on_message(msg):
  for bad_word in bad_words:
    if bad_word in msg.content.lower().split(" "):
      await msg.delete()
      bad_word_embed = discord.Embed(title = "Bad Word", description=f"{msg.author.mention}, please do not say any bad words", color = discord.Color.green())
      await msg.channel.send(embed=bad_word_embed, delete_after=5.0)

#--------------------------Errors----------------------------------------
@bot.event
async def on_command_error(ctx, error):
  if isinstance(error, commands.CommandNotFound):
    embed1 = discord.Embed(title = "Error", description = "Not a valid command", color = discord.Color.green())
    await ctx.send(embed = embed1)

#------------------------Keeps Bot Alive---------------------------------
bot.run(token)


Tags: importasyncdefbotmsgembedawaitcommand
1条回答
网友
1楼 · 发布于 2024-09-29 19:34:40

您需要将await bot.process_commands(message)添加到on_消息事件中

正如Docs告诉我们的:

This function processes the commands that have been registered to the bot and other groups. Without this coroutine, none of the commands will be triggered.

By default, this coroutine is called inside the on_message() event. If you choose to override the on_message() event, then you should invoke this coroutine as well.

This is built using other low level tools, and is equivalent to a call to get_context() followed by a call to invoke().

@bot.event
async def on_message(message):
    await bot.process_commands(message)
    for bad_word in bad_words:
        ...

相关问题 更多 >

    热门问题