The bot should sent a message but it doesn't - discord.py

So I got this script, and it doesn't work but does not write any errors in console. Just doesn't send message at all.
async def run(ctx,channel,arg1,arg2,arg3):
color = arg3
if color in color_table:
actual_color = getattr(discord.Colour, color)()
embed=discord.Embed(title=arg1, description=arg2, color=actual_color)
embed.set_footer(text="Powered by T.A.D.S.")
channel_id = channel.id
channel = discord.utils.get(ctx.guild.text_channels, id=int(channel_id))
await channel.send(embed=embed)
else:
await ctx.channel.send("This color doesn't exist in discord databeses. Type !colours to show all colors that are available.")
I want the bot to send a message to specified channel but it does nothing.

Related

You want a bot like voicemaster. but it doesn't work. Deleting a channel doesn't work as expected

There is an error in the channel deletion part. I want to create a new dedicated channel when I enter a certain channel and delete that channel when I leave that channel. However, the function to be deleted when entering and exiting a voice room is applied to all voice channels. I'm sorry for my lack of English skills.
async def on_voice_state_update(member, before, after):
guild = bot.get_guild(guildid)
category = get(guild.categories, name="╭──── 🎮 gameroom 🎮 ────╮")
try:
if after.channel.id == 1015799007995510868:
global channel
channel = await guild.create_voice_channel(str(member).split("#")[0] + "'s channel", category = category, overwrites = {member: discord.PermissionOverwrite(manage_channels=True)})
await member.move_to(channel)
except:
if len(before.channel.members) == 0:
try: await bot.get_channel(before.channel.id).delete()
except: pass```

How to use reaction as button in discord.py

If we have a message with ⬇️ and ⬆️ Reaction.
How can we get all users reacted in particular emojis and how to use as button.
Like,
If a user reacts his name will be added in message along with the emoji which he reached.
Here is a simple example this is not the best method since this will be active on all bot messages when someone reacts with ⬆️. The best is save the message id that you want then do a check here to make sure it is the wanted message.
If message.id is not an option then make a dedicated channel for this and hard code the id of that channel, this is not the best practice but it will work.
#bot.event
async def on_raw_reaction_add(payload):
channel = bot.get_channel(payload.channel_id)
message = channel.get_message(payload.message_id)
# guild = bot.get_guild(payload.guild_id)
emoji = payload.emoji.name
# skip DM messages
if isinstance(channel, discord.DMChannel):
return
# only work if in bot is the author
# skip messages not by bot
# skip reactions by the bot
if message.author.id != bot.user.id or payload.member.id == bot.user.id:
return
if emoji == '⬆️':
up_users = f"{message.content} \n {user.name}"
await message.edit(up_users)
# remove user reaction
reaction = discord.utils.get(message.reactions, emoji=emoji)
await reaction.remove(payload.member)

Discord PY Bot not responding to an event

I have this bot that when it detects that a user has written a certain message in a certain channel, it will send a message then delete the user's message and its own message.
I wrote the exact same function to do the same thing in another channel and it works just fine.
I checked the bot's permissions and it can send messages in the channel,
here is the code:
the for loop is for searching for a specific text.
#client.event
async def on_message(message):
text = message.content
chann = message.channel.id
yes = False
for strf in comms:
if text.startswith(strf):
yes = True
if yes and (message.author.id != 769302967803183124) and (message.channel.id == 331562608467509249):
print(message.author.id)
msg = await message.channel.send('mech lahne tekteb el commands ye bhim')
await message.delete()
await asyncio.sleep(4)
await msg.delete()

Discord.py logging messages

I've got this code, which when I type !snapshot should log the last 100 messages in the channel, and make them into a text file:
snapshot_channel = (my snapshot channel ID)
#commands.has_permissions(administrator=True)
#bot.command()
async def snapshot(ctx):
channel = bot.get_channel(snapshot_channel)
await ctx.message.delete()
messages = message in ctx.history(limit=100)
numbers = "\n".join(
f"{message.author}: {message.clean_content}" for message in messages
)
f = BytesIO(bytes(numbers, encoding="utf-8"))
file = discord.File(fp=f, filename="snapshot.txt")
await channel.send("Snapshot requested has completed.")
await channel.send(file=file)
I've got BytesIO imported, and it works fine for a different command which purges messages and logs them, but this code which should just make a log and then send it in the channel doesn't work. Please can you send me what it should look like for it to work. Thanks!
TextChannel.history is an async generator, you're not using it properly
messages = await ctx.channel.history(limit=100).flatten()
numbers = "\n".join([f"{message.author}: {message.clean_content}" for message in messages])
Another option would be
numbers = ""
async for message in ctx.channel.history(limit=100):
numbers += f"{message.author}: {message.clean_content}"

How to welcome new members in a specific "welcome" text channel discord.py?

I want to make a command for my bot in which we can configure the welcome message the bot can send. So, the configuration works like so: nb welcome <#channel_where_welcome_msg_to_be_shown> <#channel_to_be_mentioned_in_the_welcome_msg>.
The expected output being an embed where it is written
Hello <member>! Pls go to <#channel_to_be_mentioned_in_the_welcome_msg> to choose your roles.
Here is my code:
#client.command(aliases = ["welcome"])
async def _welcome(ctx, channel : discord.TextChannel, roles : discord.TextChannel = None):
global channel_welcome
global role_welcome
channel_welcome = channel
if roles != None:
role_welcome = roles
else:
role_welcome = None
await ctx.send("Ok, welcome message configured!")
#client.event
async def on_member_join(member):
global channel_welcome
global role_welcome
pfp = member.avatar_url
if role_welcome == None:
embedVar = discord.Embed(title="WELCOME!",description = "{}, you are welcome to this server!" . format(member.mention), color = 0xff9900)
embedVar.set_thumbnail(url = pfp)
await client.getchannel(channel_welcome).send(embed = embedVar)
else:
embedVar = discord.Embed(title="WELCOME!",description = "{}, you are welcome to this server! Go to {} to assign yourself some roles." . format(member.mention, role_welcome.mention), color = 0xff9900)
embedVar.set_thumbnail(url = pfp)
await client.getchannel(channel_welcome).send(embed = embedVar)
So, the welcome command is the configuration command. As you can see, the roles argument is optional and the user can use it if he/she wants.
Whenever I run the code and someone joins the server, it doesn't send the message or raises any error.
Any suggestions of how to fix this?
The reason why the bot does not react when a user joins, is probably because you did not define the appropriate intents for the bot.
First of all, you need to go to the Discord Developer Portal and enable the Server Members Intent
Now in the code, you need to define the intents
import discord
from discord.ext import commands
intents = discord.Intents().default()
intents.members = True
client = commands.Bot(prefix = "your_prefix", intents = intents)
This will allow the bot to listen to the on_member_join events.

Resources