我正在尝试创建一个日志bot,用于将消息存储在文本文件中。但我希望它集中在一个特定的文本频道进行监控。我有什么办法可以这样做吗
代码:
import discord
class MyClient(discord.Client):
async def on_ready(self):
print('Logged on as', self.user)
async def on_message(self, message):
file = open("logs.txt", "a", encoding="utf-8")
if message.author != self.user:
file.write(str(message.author) + ": " + message.content + "\n\n")
else:
return
file.close()
if message.content == "!logs":
file = open("logs.txt", encoding="utf-8")
embed = discord.Embed(title="Logs in this channel", description="All Messages In This Channel")
embed.add_field(name='logs', value=file.read())
await message.channel.send(content=None, embed=embed)
file.close()
if message.content == "!deletelogs":
with open("logs.txt", "w", encoding="utf-8"):
pass
if message.content == "!lockbothere":
# Lock Bot To TextChannel
bot = MyClient()
bot.run("lolno")
您可以只检查消息通道并使用id或名称
如果您输入以下打印语句,您将看到这些值,然后可以根据需要添加条件语句
message.channel
将返回discord.channel.TextChannel对象(用于文本频道)message.channel.id
将返回通道id的整数值message.channel.name
将返回通道名称的字符串值(如“general”)相关问题 更多 >
编程相关推荐