嘿,我是 discord.py 的新手,我想知道是否有办法将服务器中发送的所有消息记录到频道中。 (我的机器人在服务器中,是的)
例如有人在服务器中发送消息,我的机器人记录消息并将其发送到频道
答案 0 :(得分:0)
我使用注释来解释代码,所以请仔细阅读。
# this event will be triggered whenever a message is received
@client.event
async def on_message(msg):
# log is a coroutine, so don't forget to enable tracemalloc for the call
await log(msg)
# to avoid 'commands not working'
client.process_commands(msg)
async def log(msg):
# first we will get the guild from the message
guild = msg.guild
# then we will find the channel with name 'logs'
log_channel = discord.utils.get(guild.channels, name="logs")
try:
# if the channel exists and the bot has permissions
# to send messages in 'logs' channel, this will pass,
# else an exception will be thrown
await log_channel.send("*{0}*, sent by **{0.author.nick}**, in **msg.channel.name**")
except:
# currently i'm using bare except, but you can specify the exceptions
# the exceptions will be raised if any of those said above, are missing
print("'logs' channel not found, or bot missing permissions")