如何修复 Discord.py 中的未知消息(404)
2020-07-25
5126
我的机器人使用 Discord.py 自动对新消息做出反应,并在 25 颗星后将其添加到 starboard。但是经过一段时间的运行后,会出现此错误: 忽略 on_raw_reaction_add 中的异常
Ignoring exception in on_raw_reaction_add
Traceback (most recent call last):
File "C:\Users\timpi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 312, in _run_event
await coro(*args, **kwargs)
File ".\main.py", line 36, in on_raw_reaction_add
message = await reactchannel.fetch_message(payload.message_id)
File "C:\Users\timpi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\abc.py", line 935, in fetch_message
data = await self._state.http.get_message(channel.id, id)
File "C:\Users\timpi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\http.py", line 223, in request
raise NotFound(r, data)
discord.errors.NotFound: 404 Not Found (error code: 10008): Unknown Message
这是
on_raw_reaction_add
事件:
@bot.event
async def on_raw_reaction_add(payload):
channel_id = int(config["channelIDMemephone"])
reactchannel = bot.get_channel(channel_id)
message = await reactchannel.fetch_message(payload.message_id)
await star_post_check(message)
这是完整代码:
import discord, json
from discord.ext import commands
# Loading the config from a JSON file
config = json.load(open('config.json'))
# Linking variables to the config.json
react_channel = int(config["channelIDMemephone"])
starboard_channel_id = int(config["channelIDStarboard"])
star_emoji = config["powerstar"]
required_stars = int(config["starboardlimit"])
token = config["token"]
# Bot "metadata"
bot = commands.Bot(command_prefix='$', description="A star bot.")
# Execute this as the bot logs in
@bot.event
async def on_ready():
print('Logged in as {}'.format(bot.user.name))
await bot.change_presence(activity=discord.Game(name='with stars'))
# Add a star to every new message in the specified channel
@bot.event
async def on_message(message):
if int(message.channel.id) == react_channel:
await message.add_reaction(star_emoji)
#Call the star_post_check() function on every added reaction
@bot.event
async def on_raw_reaction_add(payload):
channel_id = int(config["channelIDMemephone"])
reactchannel = bot.get_channel(channel_id)
message = await reactchannel.fetch_message(payload.message_id)
await star_post_check(message)
#Checking if the post has more than 25 Stars and if so sending an embed to the starboard channel
async def star_post_check(message: discord.Message):
if str(message.id) in open('sent.txt').read():
match = True
else:
match = False
if match:
return
add_to_starboard = False
starboard_channel = discord.utils.get(message.guild.channels, id=starboard_channel_id)
for i in message.reactions:
if i.emoji == star_emoji and i.count >= required_stars and message.channel != starboard_channel:
add_to_starboard = True
if add_to_starboard:
# embed message itself
starboard_embed = discord.Embed(title='Starred post', description=message.content, colour=0xFFD700)
starboard_embed.set_author(name=message.author, icon_url=message.author.avatar_url)
try:
if message.content.startswith('https://'):
starboard_embed.set_image(url=message.content)
except:
pass
try:
starboard_embed.set_image(url = message.attachments[0].url)
except:
pass
# sending the actual embed
await starboard_channel.send(embed=starboard_embed)
cache = open("sent.txt", "a")
cache.write(str(message.id) + " ")
cache.close()
bot.run(token)
1个回答
如果消息不在您的配置文件中的频道中,则您在搜索该频道时将找不到它。您可以添加检查以确保您只与该频道中的消息进行交互:
@bot.event
async def on_raw_reaction_add(payload):
if payload.channel_id == int(config["channelIDMemephone"]):
reactchannel = bot.get_channel(payload.channel_id)
message = await reactchannel.fetch_message(payload.message_id)
await star_post_check(message)
Patrick Haugh
2020-07-25