简体   繁体   中英

Discord.py on_message causing loop

I have encountered a problem while making a bot in discord.py, I managed to get the commands and on_message to work alongside each other, with quite a bit of difficulty, but now all the commands work but if i trigger one of the on_message functions, it loops the output for no clear reason. I am unsure on how to fix this issue, nor identify it.

import discord
from discord.ext import commands
import random
import os
import time
import typing

bot = commands.Bot(command_prefix = '!')

@bot.command()
async def test(ctx):
    await ctx.send('test')

with open("words.txt") as file:
    words = [word.strip().lower() for word in file.readlines()]
@bot.event
async def on_message(message):
    message_content = message.content.strip().lower()
    for word in words:
        if word in message_content:
            await message.channel.send(f"<CUSTOMSERVEREMOJI>")
bot.run('TOKEN')

The on_message(message) function should include 2 items to work smoothly.

  1. At the beginning skip messages from the bot itself - with something like the following:
if message.author == bot.user:
    return
  1. At the end process commands (since a command is processed by on_message first) - with something like
await bot.process_commands(message)

I have encountered a problem while making a bot in discord.py, I managed to get the commands and on_message to work alongside each other, with quite a bit of difficulty, but now all the commands work but if i trigger one of the on_message functions, it loops the output for no clear reason. I am unsure on how to fix this issue, nor identify it.

import discord
from discord.ext import commands
import random
import os
import time
import typing

bot = commands.Bot(command_prefix = '!')

@bot.command()
async def test(ctx):
    await ctx.send('test')

with open("words.txt") as file:
    words = [word.strip().lower() for word in file.readlines()]
@bot.event
async def on_message(message):
    message_content = message.content.strip().lower()
    for word in words:
        if word in message_content:
            await message.channel.send(f"<CUSTOMSERVEREMOJI>")
bot.run('TOKEN')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM