Wafaduck
Wafaduck

Reputation: 25

How to sent a bot message to multiple channels in one server (Read the body for more information)

it's about alerting to a different discord server, so I send a command to this one server and it will send out to all the servers my bot is in, In a specific channel and pinging a specific role. but my new issue is that I want my bot to alert in this 1 certain server on 3 different channels at the same time but it doesn't work. It only alerts to one channel each in every server. (**UPDATE! This is my alternative that I found and it seems like the bot is running but the commands wouldn't work, I don't even get any error)

If you're confused, #Bangers Den (server name) / Low Float , Bangers Mid Large, Bangers Forex (These are the channel names). Same goes for #Banger Alerts ( different server)

Here's my current code! :)

@commands.has_permissions(kick_members=True)
async def alert(ctx, *, msg):#ALERT
    
    data = {
  922874978528034886 : [922875504959295579, 922874978528034888], #Banger Alerts / Server ID, Channel Name, Role ID
  884931761610563595 : [921234942405525504, 884974424468623411], #Bangers Den / Low Float
  884931761610563595 : [921890822100758528, 884974424468623411], #Bangers Den / Bangers Mid Large
  884931761610563595 : [921907840967397476, 884974424468623411], #Bangers Den / Bangers Forex
  }

    for guild_id in data.keys():
        
        guild = discord.utils.get(client.guilds, id=guild_id)
        channel = discord.utils.get(guild.channels, id=data [guild_id][0])
        role = discord.utils.get(guild.roles, id=data [guild_id][1])  
        one = Button(style=ButtonStyle.URL, label='Twitter', url="https://twitter.com/Wafaduck")
        embed=discord.Embed(title= ':moneybag: **Alert** :moneybag:', description= (msg), color=0x00FF00, timestamp=datetime.datetime.utcnow())
        embed.set_thumbnail(url='https://cdn.discordapp.com/attachments/872295537544679484/922881014852898826/IMG_20211221_001839.jpg')
        embed.set_footer(icon_url = ctx.author.avatar_url, text='Powered by Duck Programming',)
        await channel.send(content=f"{role.mention}")                 
        await channel.send(embed=embed)```

**UPDATED**
Here's the updated one where I'm facing an issue that the bot runs but the commands wouldn't work,  I don't even get any error.
```@client.command() #v1
@commands.has_permissions(kick_members=True)
async def alert(ctx, *, msg):#ALERT
    
    data = {
  884931761610563595 : [[921234942405525504, 884974424468623411, 922895552155357214], #Bangers Den / Low Float / Premium / 2 roles 
                        [921890822100758528, 884974424468623411, 922895552155357214], #Bangers Den / Mid Large / Premium / 2 roles 
                        [921907840967397476, 884974424468623411, 922895552155357214]],#Bangers Den / Forex / Premium  / 2 roles
  875629371824697384 : [[923064332156276756, 900029600028033074]] #Different server with one role.
  
   }    

    for guild_id, entries in data.items():
        for channel_id, role_ids in entries:
            guild = discord.utils.get(client.guilds, id=guild_id)
            channel = discord.utils.get(guild.channels, id=channel_id)
            role = (discord.utils.get(guild.roles, id=role_id).mention for role_id in role_ids) 
            embed=discord.Embed(title= '💰 **Alert** 💰', description= (msg),color=0x00FF00, timestamp=datetime.datetime.utcnow())
            embed.set_thumbnail(url='https://cdn.discordapp.com/attachments/872295537544679484/922881014852898826/IMG_20211221_001839.jpg')
            embed.set_footer(icon_url = ctx.author.avatar_url, text='Powered by Duck Programming',)
            await channel.send(content=f"{role}")                 
            await channel.send(embed=embed)```

Upvotes: 2

Views: 358

Answers (1)

Nathan Marotte
Nathan Marotte

Reputation: 806

The issue you are facing is that a dictionary (your data variable) will squash all its content with the same key.

You can see that by printing the variable data right after defining it

>>> data = {
...   922874978528034886 : [922875504959295579, 922874978528034888], #Banger Alerts / Server ID, Channel Name, Role ID
...   884931761610563595 : [921234942405525504, 884974424468623411], #Bangers Den / Low Float
...   884931761610563595 : [921890822100758528, 884974424468623411], #Bangers Den / Bangers Mid Large
...   884931761610563595 : [921907840967397476, 884974424468623411], #Bangers Den / Bangers Forex
...   }
>>> data
{922874978528034886: [922875504959295579, 922874978528034888], 884931761610563595: [921907840967397476, 884974424468623411]}
>>> data.keys()
dict_keys([922874978528034886, 884931761610563595])

You see that data.keys() is just a list of 2 elements

You will have to rethink your data variable structure, and maybe make use of JSON too, it could be useful :)

Upvotes: 1

Related Questions