Discord.py: Create an 'on_message' Function for Fun Chat Interactions
Sure! Here's an example of an on_message function using discord.py that responds to certain messages with a fun reaction:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
@bot.event
async def on_message(message):
# Ignore messages from the bot itself to avoid infinite loops
if message.author == bot.user:
return
# Check if the message content matches a specific trigger
if message.content.lower().startswith('hello'):
await message.channel.send('Hello there!')
elif message.content.lower().startswith('goodbye'):
await message.channel.send('Goodbye!')
elif message.content.lower().startswith('!react'):
await message.add_reaction('👍')
await message.add_reaction('👎')
# Let the bot process commands as well
await bot.process_commands(message)
# Run the bot
bot.run('YOUR_BOT_TOKEN')
In this example, the bot responds with different messages based on the triggers 'hello' and 'goodbye'. Additionally, if a message starts with !react, the bot will add a thumbs-up and thumbs-down reaction to that message.
Remember to replace 'YOUR_BOT_TOKEN' with your actual bot token.
原文地址: https://www.cveoy.top/t/topic/o8dg 著作权归作者所有。请勿转载和采集!