简体   繁体   English

Discord.py 从频道读取消息

[英]Discord.py read messages from channels

I m trying to listen to all messages from specific channels inside guilds that im not owner, i started to trying listen to all messages with the code below, without success.我正在尝试收听来自我不是所有者的行会内特定频道的所有消息,我开始尝试使用下面的代码收听所有消息,但没有成功。 How can i do that if possible using the channel id?如果可能的话,我该如何使用频道 ID?

Thank you!谢谢你!

import discord

client = discord.Client()
guild = discord.Guild
messages =  []


print("Bot started")
@client.event
async def on_message(message):

    msg = message.content

    if msg != "" :
 
        messages.append(msg)

    while True:
        print(messages)
    
client.run('TOKENHERE')

Every message object has the channel it was sent in as an attribute.每条message object 都有发送它的通道作为属性。 You can simply compare the ids, and if they match, run your code.您可以简单地比较 id,如果它们匹配,则运行您的代码。
Also be sure that you have the messages intent, in order for the on_message() event to work, like said here .还要确保你有messages意图,以便on_message()事件起作用,就像这里说的那样。

import discord

intents = discord.Intents.default()
intents.messages = True

client = discord.Client(intents = intents)
guild = discord.Guild
messages =  []


print("Bot started")
@client.event
async def on_message(message):
    
    channelIDsToListen = [ 12345, 54321 ] # put the channels that you want to listen to here

    if message.channel.id in channelIDsToListen:

        if message.content != "" :
 
            messages.append(message.content)

        print("New message: " + message.content)
    
client.run('TOKENHERE')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM