简体   繁体   中英

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?

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. You can simply compare the ids, and if they match, run your code.
Also be sure that you have the messages intent, in order for the on_message() event to work, like said here .

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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