在cogs discord中狙击命令,py



我正在用齿轮重新设计我的不和谐机器人,我被困在狙击功能上:

import discord
from discord.ext import commands
class Snipe(commands.Cog):
def __init__(self, client):
self.Client = client
@commands.Cog.listener()
async def on_ready(self):
print ("Snipe is now enabled")
async def on_message_delete(self, message):
messageauthor = {}
messagecontent = {}
messageauthor[message.channel.id] = message.author
messagecontent[message.channel.id] = message.content
@commands.command()
async def snipe(self, ctx):
channel = ctx.channel
try:
em = discord.Embed(description = f"said:n{ctx.messagecontent[channel.id]}", color = 0x00c230)
em.set_author(name = f"Last deleted message in #{ctx.channel.name}")
em.set_thumbnail(url="https://cdn.discordapp.com/avatars/352793093105254402/8a2018de21ad29973696bfbf92fc31cd.png?size=4096")
em.set_footer(text = f"Snipe requested by {ctx.message.author}")
await ctx.channel.send(embed = em)
except:
embed = discord.Embed(colour = 0x00c230)
embed.set_author(name=f"There are no deleted messages in #{ctx.channel.name}!")
embed.set_thumbnail(url="https://cdn.discordapp.com/avatars/352793093105254402/8a2018de21ad29973696bfbf92fc31cd.png?size=4096")
embed.set_footer(text=f"Snipe requested by {ctx.message.author}")
await ctx.channel.send(embed=embed)
def setup(client):
client.add_cog(Snipe(client))

到目前为止,这是我的代码,这是似乎不工作的部分:

@commands.command()
async def snipe(self, ctx):
channel = ctx.channel
try:
em = discord.Embed(description = f"said:n{ctx.messagecontent[channel.id]}", color = 0x00c230)
em.set_author(name = f"Last deleted message in #{ctx.channel.name}")
em.set_thumbnail(url="https://cdn.discordapp.com/avatars/352793093105254402/8a2018de21ad29973696bfbf92fc31cd.png?size=4096")
em.set_footer(text = f"Snipe requested by {ctx.message.author}")
await ctx.channel.send(embed = em)

当我试图删除已删除的消息时,它只显示"没有消息要删除"的文本。选择。如果有人可以帮助这个/有自己的代码,编辑它,尽可能多的你想要的,我会解决这个问题。谢谢!

在python中,如果你在函数中赋值一个变量,它将被视为一个局部变量。要使其全局化,您需要使用global关键字。但是,由于您是在类中编写代码,因此可以将变量分配给该类,如:

# to assign
self.messageauthor = {}
# to use
print(self.messageauthor)
所以你的新代码将是:
@commands.command()
async def snipe(self, ctx):
channel = ctx.channel
try:
em = discord.Embed(title="Put something here", description = f"said:n{ctx.messagecontent[channel.id]}", color = '0x00c230') #color code should be a str
em.set_author(name = f"Last deleted message in #{ctx.channel.name}")
em.set_thumbnail(url="https://cdn.discordapp.com/avatars/352793093105254402/8a2018de21ad29973696bfbf92fc31cd.png?size=4096")
em.set_footer(text = f"Snipe requested by {ctx.message.author}")
await ctx.channel.send(embed = em)
except:
embed = discord.Embed(title="", description="", colour = '0x00c230') # should be str
embed.set_author(name=f"There are no deleted messages in #{ctx.channel.name}!")
embed.set_thumbnail(url="https://cdn.discordapp.com/avatars/352793093105254402/8a2018de21ad29973696bfbf92fc31cd.png?size=4096")
embed.set_footer(text=f"Snipe requested by {ctx.message.author}")
await ctx.channel.send(embed=embed)

另外,titledescription是必需的参数来进行嵌入。并且,html颜色代码应该是一个str(使用")。我希望这些对你有帮助。