簡體   English   中英

在 discord.py 的特定事件中向所有公會發送消息

[英]sending message in all guilds on certain event in discord.py

我使用此代碼向名為“giveaway-index”的某個頻道發送消息,但問題是它只在一個公會中向名為“giveaway-index”的頻道發送消息。 我希望它將消息發送到所有具有名為“giveaway-index”的頻道的公會。 有什么辦法可以做到嗎?

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if "GIVEAWAY" in message.content:
      if message.author.id == 294882584201003009:
        global channel_id
        channel = discord.utils.get(message.guild.channels, name='giveaway-index')
        channel_id = channel.id
        print(channel_id) 
        channel = client.get_channel(channel_id)
        await channel.send("blah blah blah")

for guild in client.guilds循環遍歷所有的公會。 然后你只需要檢查帶有“giveaway-index”的頻道是否在公會中並發送消息。

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if "GIVEAWAY" in message.content:
        if message.author.id == 294882584201003009:
            for guild in client.guilds: # looping through all guilds your bot is in
                channel = discord.utils.get(message.guild.text_channels, name='giveaway-index') # getting channel
                if channel: # checking if channel is in the guild
                    await channel.send("blah blah blah")
@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if "GIVEAWAY" in message.content:
        if message.author.id == 294882584201003009:
            for guild in client.guilds: 
                channel = discord.utils.get(guild.text_channels, name='giveaway-index') 
                if channel: 
                    await channel.send("blah blah blah")

這個對我有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM