python - 不和谐蟒蛇多次狙击
问题描述
我有一个不和谐的蟒蛇狙击机器人,但他们只狙击一次。我希望它能够多次狙击。我想用 deque 来做,但我不知道怎么做,因为我是 Python 新手。
import discord
from discord.ext import commands
bot.sniped_messages = {}
@bot.event
async def on_message_delete(message):
bot.sniped_messages[message.guild.id] = (message.content, message.author, message.channel.name, message.created_at)
@bot.command()
async def snipe(ctx):
try:
contents, author, channel_name, time = bot.sniped_messages[ctx.guild.id]
except:
await ctx.channel.send("er is geen verwijderde bericht Dombo!")
return
embed = discord.Embed(description=contents, color=discord.Color.red(), timestamp=time)
embed.set_author(name=f"{author.name}#{author.discriminator}", icon_url=author.avatar_url)
embed.set_footer(text=f"Deleted in : #{channel_name}")
await ctx.channel.send(embed=embed)
bot.run(TOKEN)
解决方案
使用列表保存所有已删除的消息:
import discord
from discord.ext import commands
bot = commands.Bot(prefix="-")
sniped_messages = {}
@bot.event
async def on_message_delete(message):
history = sniped_messages.get(message.guild.id, [])
history.insert(0, message)
sniped_messages[message.guild.id] = history
@bot.command()
async def snipe(ctx, position):
try:
message = sniped_message[ctx.guild.id][position-1]
except (KeyError, IndexError):
await ctx.channel.send("er is geen verwijderde bericht Dombo!")
return
embed = discord.Embed(
description=message.content,
color=discord.Color.red(),
timestamp=message.created_at
)
embed.set_author(name=f"{message.author}", icon_url=message.author.avatar_url)
embed.set_footer(text=f"Deleted in : {message.channel}")
await ctx.channel.send(embed=embed)
bot.run(TOKEN)
但我建议您#logs
在公会中创建一个频道,您的机器人将发送所有已删除的消息:
import discord
from discord.ext import commands
MY_GUILD_ID = 623204625251827724
LOGS_CHANNEL_ID = 626136590305198113
bot = commands.Bot(prefix="-")
@bot.event
async def on_message_delete(message):
if message.guild.id != MY_GUILD_ID:
return
embed = discord.Embed(
description=message.content,
color=discord.Color.red(),
timestamp=message.created_at
)
embed.set_author(name=f"{message.author}", icon_url=message.author.avatar_url)
embed.set_footer(text=f"Deleted in : {message.channel}")
logs = bot.get_channel(LOGS_CHANNEL_ID)
await logs.send(embed=embed)
bot.run(TOKEN)
推荐阅读
- qt - 如何从 QSortFilterProxyModel 上的按列排序中排除一行
- ansible - 在 Ansible 中使用嵌套列表迭代字典列表
- reactjs - React.Children.only 期望接收单个 React 元素子元素。?
- javascript - rotating player camera on a sphere (javascript/three.js)
- angular - cordova-plugin-firebase-dynamiclinks :导出声明类型 ObservedValueOf
= O 扩展 ObservableInput ? T:从不; - php - 将集合中匹配的单词存储在数据库中
- c# - LINQ:按键从字典中获取项目及其值,然后将它们分配给变量
- python - 需要 Microsoft Visual C++ 14.0 - 为 python 安装 mysqldb
- python-3.x - 操作熊猫数据框 - 有条件的
- git - git 提交失败