python - 如何通过命令关闭事件?不和谐.py
问题描述
我目前正试图在机器人响应中做出反应,以关闭一个监听被删除消息的审核事件。这将用作过滤器,因此如果用户不想记录已删除的消息,他们可以通过反应将其关闭。我听说过wait for
anddispatch
函数,但我目前不知道它是如何工作的,也找不到教程。任何反馈都会令人惊叹,因为我有点卡住并愿意倾听任何人的声音。
当前代码;
import discord
from discord.ext import commands
import datetime
class log(commands.Cog):
def __init__(self,bot):
self.bot = bot
mess = False
@commands.Cog.listener()
async def on_reaction_add(self, reaction, user):
channel = reaction.message.channel
guild = channel.guild
message = reaction.message
if user != self.bot.user:
if reaction.emoji == '1️⃣':
check = discord.utils.get(guild.categories, name="Chilly Logs") # Find the Category
if not check: # If category doesnt exist create the logs
cat = await channel.guild.create_category_channel('Chilly Logs')
await channel.guild.create_text_channel("Message Logs", category=cat) # Message Logs
await channel.guild.create_text_channel("Other Logs", category=cat) # Other Logs
await channel.send('**Log Channels Were Succesfully Added!**')
await message.remove_reaction('1️⃣', user)
return
else:
await channel.send('**Log Channel Already Exists**')
await message.remove_reaction('1️⃣', user)
if reaction.emoji == '2️⃣':
channel1 = discord.utils.get(guild.channels, name="other-logs") # Other Logs
channel2 = discord.utils.get(guild.channels, name="message-logs") # Message Logs
category = discord.utils.get(guild.categories, name="Chilly Logs") # Category/Parent
if category is not None: # Deletes All The Channels
await channel1.delete()
await channel2.delete()
await category.delete()
await channel.send('**Logging Channels Have Been Removed**')
await message.remove_reaction('2️⃣', user)
else:
await channel.send('**Channels Either Dont Exist Or Have Been Renamed**')
await message.remove_reaction('2️⃣', user)
if reaction.emoji == '❗':
@commands.command()
async def test(self, ctx):
embed = discord.Embed(title = "Chilly Logging", description = "Chilly Will Log Any Edited, Deleted Messages. More Features & Flexibility Coming Soon", colour = discord.Color.blurple())
embed.add_field(name="Logging Commands ", value="1️⃣ - Turn On Server Logging", inline=True)
embed.add_field(name=" ", value= "2️⃣ - Delete & Turn Off Logging", inline=False)
msg = await ctx.send(embed=embed)
emoji = ['1️⃣', '2️⃣', '❗']
response = 3
for i in range(response):
await msg.add_reaction(emoji[i])
@commands.Cog.listener()
async def on_message_delete(self, message):
if x == True:
if not message.author.bot: # Checks for bot message
channel = message.channel.name # Channel the deleted message is from
logchannel = discord.utils.get(message.guild.channels, name='message-logs') # Finds the log channel
embed = discord.Embed(title="Message Log", description="", color= discord.Color.red()) # Embeds
embed.add_field(name="Message sent by {} has been deleted in `{}`" .format(message.author.display_name, channel), value=message.content, inline=True,)
embed.set_footer(text='User ID: {} | Message ID: {}' .format(message.author.id, message.id))
await logchannel.send(embed=embed) # Finally sends the embed to log channel
@commands.Cog.listener()
async def on_message_edit(self, before, after):
if not after.author.bot:
if before.content != after.content:
channel = after.channel.name # Channel the edited message is from
logchannel = discord.utils.get(after.guild.channels, name='message-logs') # Finds the log channel
embed = discord.Embed(title="Message Log", description="Message edited in `{}` by {}" .format(channel, after.author.display_name), color= discord.Color.red()) # Embeds
embed.add_field(name="Before", value=before.content, inline=True,)
embed.add_field(name="After", value=after.content, inline=False,)
embed.set_footer(text='User ID: {} | Message ID: {}' .format(after.author.id, before.id))
await logchannel.send(embed=embed) # Finally sends the embed to log channel
def setup(bot):
bot.add_cog(log(bot))```
解决方案
您可以使用全局变量。简单地:
Toggle = True
@bot.command()
async def toggle(ctx):
global Toggle
# Toggles the variable.
Toggle = !Toggle
# Example event
@bot.event
async def on_message(message):
if Toggle:
# Event's code
else:
await message.channel.send("Function disabled")
推荐阅读
- java - BottomNavigationView 未显示超过 3 个项目
- mysql - 如果没有详细数据,MariaDB 从明细表中减去或返回主值
- python - 生成一个json文件
- java - 如何在java中全球化方法参数?
- vue.js - vuejs 中的 beforeMount 和 created 生命周期钩子有什么区别
- html - 输入数据而不专注于输入。角 10
- python - python中的逻辑回归测试输入格式帮助
- sql - 表 2 中的左连接
- javascript - 在 AntDesign 中将样式应用于外部库组件
- database - 收到新消息时刷新 Wix 中继器