I have a discord bot that will scrape covid information and send the information to a specific discord server's text channel. But how can I make the bot to send the data to all server's text channel which it connected ?
Currently I'm specifying which text channel:
channel = client.get_channel(694814300220686399) # replace with channel id of text channel in discord
client.loop.create_task(channel.send("abc")
Instead of only sending to a specific server's text channel, I want to send it to every channel it has access to.
For this, you'll need a channel with a common name in all the servers... Let's say the name of the channel is 'general'. Then, you can do it this way...
import discord
from discord.ext import commands
for server in client.guilds:
requiredChannel = discord.utils.get(server.channels, name = 'general')
await requiredChannel.send("Your message")
you can do like this:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix=".")
#client.event()
async def on_ready():
#client.command()
async def send_info(ctx , heading, content):
for guild in client.guilds:
for channel in guild.channels:
if channel == "Covid Alert":
channel.send("**"+Heading+"**\n"+content)
sorry for a messy code it could have been done better i think
If I understood your question correctly this should fit your needs.
for channel in guild.text_channels:
if channel.permissions_for(guild.me).send_messages:
await channel.send("Message")
Example "covid" commmand
#client.command()
async def covid(ctx):
guild = ctx.guild
for channel in guild.text_channels: #loops through all channels
if channel.permissions_for(guild.me).send_messages: #checks for permissions
await channel.send("Message") #sends your message
Related
I am trying to send a message to a specific channel and I can run the code below just nothing show up on the channel and I have no idea why... I put the channel Id in the get_channel input, just putting a random number here.
import discord
client = discord.Client()
async def send_msg(msg):
channel = client.get_channel(123456456788)
await channel.send('hello')
Is the function even called somewhere? Otherwise there could also be an issue with the discord permissions or the Intents.
This is one of many ways to call the function and post 'hello' in some specific channel. No msg parameter is required.
#client.event
async def on_ready():
await send_msg()
async def send_msg():
channel = client.get_channel(123456456788)
await channel.send('hello')
from discord.ext import commands
import discord
bot = commands.Bot(command_prefix="!")
#bot.event
async def on_guild_channel_create(channel):
message = "This Message is sent via DM"
user = bot.get_user("My_Discord_ID")
await user.send(message)
bot.run("TOKEN")
I ran the code but nothing happened. I am not sure whats going on.
To create a private message with a user, you must use the User.create_dm() method to obtain a channel. Once you have fetched that channel, you can send content to the user.
import discord
#bot.event
async def on_guild_channel_create(channel):
user = bot.get_user("My_Discord_ID")
channel = await user.create_dm()
try: await channel.send("This Message is sent via DM")
except discord.HTTPException: print("User has DMs disabled")
I am programming a discord bot that will send an embed to a separate channel when someone uses a command to prevent abuse and so our admins can keep an eye on usage.
I have this code:
#client.command()
#has_permissions(manage_messages=True)
async def purge(ctx, amount=5):
await ctx.channel.purge(limit = amount + 1) # Plus one is so that it also deletes the players purge message
embed = discord.Embed(title="Purged Channel", description="", colour=0xe74c3c, timestamp=datetime.utcnow())
embed.set_author(name="CS Moderation", icon_url="https://cdn.discordapp.com/attachments/938857720268865546/938863881726615602/ca6.png")
embed.add_field(name="Staff Member", value=ctx.author.mention, inline=False)
embed.set_footer(text="Made by HeadOfTheBacons#6666", icon_url=None)
channel = client.get_channel("938898514656784404") #Change ID of logging channel if needed
await channel.send(embed = embed)
When I run this, I cannot get the embed to be sent to a different channel. I have tried checking other posts already but I have not had much luck at all. I have no errors when I use the command or try to run the bot. The point here is when someone uses the command to make a new embed, set up the preferences, specify the specific channel ID where I want this embed to be sent to, and have the computer send it off to that channel. However, it is not sending. Why is my code not working? What do I need to change in my code to make this send a message?
Check if you have an on_command_error function in your program, it could be the reason of your no error problem.
Also not that the embed.footer can't be None, and the client.get_channel requires an Integer.
So you can try out the following code, it should work:
from datetime import datetime
import discord
from discord.ext import commands
from discord.ext.commands import has_permissions
client = commands.Bot(command_prefix="prefix")
#client.command()
#has_permissions(manage_messages=True)
async def purge(ctx, amount=5):
await ctx.channel.purge(limit=amount + 1) # Plus one is so that it also deletes the players purge message
embed = discord.Embed(title="Purged Channel", description="", colour=0xe74c3c, timestamp=datetime.utcnow())
embed.set_author(name="CS Moderation", icon_url=client.user.avatar_url)
embed.add_field(name="Staff Member", value=ctx.author.mention, inline=False)
embed.set_footer(text="Made by HeadOfTheBacons#6666")
channel = client.get_channel(938898514656784404) # Change ID of logging channel if needed
await channel.send(embed=embed)
client.run("TOKEN")
I have a discord bot that periodically checks some data in a database and if there is an update it outputs something to the channel. So I set a function that will run every hour.
async def check_new():
general_channel = bot.get_channel(channel_id)
embed = discord.Embed(
title="Update!",
color=0xff0000,
description=''
)
while True:
change = check_change()
if change:
await general_channel.send(embed=embed)
await asyncio.sleep(3600)
#bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
bot.loop.create_task(check_new())
The issue comes when the bot is added to multiple servers where the message only gets sent to a specific to a specific channel_id so messages get sent only to my server channel. Essentially I am sending a notification to only my channel, how can I send one to every channel that has my bot, is that possible?
You can get the list of guilds your bot is in, with bot.guilds
if change:
guilds = bot.guilds()
for guild in guilds:
channels = guild.channels #get all channels
for channel in channels:
try:
await channel.send(embed=embed)
break
except:
print('unable to send in', channel, guild)
Although you can do this and it will work fine, I wouldn't recommend doing it as the bot may send the update in random channels. I suggest you ask the users to set a bot updates channel, so you can store it in a database and use it to send updates.
References:
bot.guilds
guild.channels
I am trying to make a discord bot that gives member on on_member_join read_messages = False. I am only able to achieve this when I iterate through all possible discord text channels in a for loop, but I would only want to do it for a specific channel. I know how to create a channel and give it read_messages = False, but I need to do it on a already existing channel.
My current code:
import time
import discord
from dotenv import load_dotenv
import os
import random
from discord.utils import get
import asyncio
load_dotenv()
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
#client.event
async def on_member_join(member):
if member.guild.id == 599754972228888888:
print("Correct guild. New member: " + str(member))
else:
return
guild = client.get_guild(599754972228888888)
channel = client.get_channel(517974569018888888)
print(guild.channels)
for cha in channel.channels:
await cha.set_permissions(member, read_messages=False)
client.run(DISCORD_TOKEN)
Is there any reason to not simply deny the #everyone-role reading permission for that specific channel from your Discord server settings?
Assuming channel = client.get_channel(517974569018888888) is the channel you want to adjust the permissions for, you can deny the reading permissions directly doing
await channel.set_permissions(member, read_messages=False)
If channel is not the channel you want or there's some dynamic involved, you can use discord.utils.get to get the channel by providing e.g. the channel name and then proceed with 2.
Neither of the solutions needs the for loop, it can be eliminated entirely
I think you want to make a text channel which is able to see only for administrators or verified user or smth like that.
#client.event
async def on_member_join(member):
channel = discord.utils.get(member.guild.channels, name="NAME")
await channel.set_permissions(member, read_messages=False)
If you want to set perms for everyone do this:
#client.command()
#command.has_permissions(administrator=True)
async def setperms(ctx):
guild = ctx.guild
role = discord.utils.get(guild.roles, name="#everyone")
channel = discord.utils.get(member.guild.channels, name="NAME")
await channel.set_permissions(member, read_messages=False)
Replace NAME with name of your channel