The bot don't send message in specific channel - python

I'm trying to make a partnership command and to do that I need the bot to send a message in a specific channel. This is the code:
bot.command()
async def force(ctx, *, message=None):
channel = bot.get_channel(854633281589084221)
await ctx.channel.send(message)
(Sorry for the bad translation)
In the line of code:
await ctx.channel.send(message)
If I put ctx before channel it works, but it sends the message in the channel in which the command was executed and the programme tells me that the local variable channel isn't used, while if I put it after it no longer tells me that channel isn't used but it does not work

channel already has the value of the channel you want to send the message in.
So, you just need to run await channel.send(message) or await bot.get_channel(<id>).send(message) for your bot to work.

(adding on to #TheSj)
When a command is ran, the bot already replies in that given channel.
If you want to make it so it can only reply in that channel, you can use discord permissions using roles and remove the role from each channel you don't want it in.
Hope this helps and have a great day.:)

Related

Discord bot only connects to voice channel that the user was in when the bot runs

I am currently trying to create a code that requires my bot to identify the current voice channel the user is in and retrieve what users are in that channel. However the bot will only identify a voice channel that the user was in when i run the script. It will also return the same voice channel even if the user has left. And if i run the script before i join the voice channel it will say the user is not in a channel at all. Why does my code sometimes recognise a user is in a voice channel and sometimes not?
This is the current code im using for testing:
#bot.command()
async def check(ctx):
channel = ctx.author.voice.channel
members = channel.voice_states
print(members)
I'm running the script off Replit (something tells me this could be my the hosting source or something i don't know this has caused me too many headaches).
I tried to make my explanation better but if its still bad please tell me ill go fetch a friend who can help me fix it.
you have the right idea retrieving the channel. but you also want to check to make sure the user invoking the command is actually in a voice channel.
#bot.command()
async def check(ctx):
channel = ctx.author.voice.channel
if channel is None:
return await ctx.send("You are not connected to a voice channel")
else:
memList = []
for member in channel.members:
memList.append(member.name)
Now you have a list of members connected to that voice channel.

If Discord.py Bot gets dm

Dear stackoverflow community.
I want to add a little 'Easter egg' to my discord.py bot. If one gets a DM from a user, he should reply something like "No private messages while at work". Is that somehow possible? Unfortunately, I have no idea how to do this. That's why I can't attach code. Please help me : )
You can have an on_message event, which will fire each time there is any new message which your bot can read. Then you can check if the message is a direct message:
#bot.event # could be client instead of bot for you
async def on_message(message):
if message.author == bot.user: # Don't reply to itself. Could again be client for you
return
if isinstance(message.channel,discord.DMChannel): #If you want this to work in a group channel, you could also check for discord.GroupChannel
await message.channel.send("No private messages while at work")
await bot.process_commands(message)
Also don't forget to add bot.process_commands(message) add the end of on_message so your commands still work. (Could again be client instead of bot for you)
References:
Message.channel

How to send welcome message in first text channel discord.py

I've got a pretty decent bot that I'm planning to launch soon. It's called moderator and it's supposed to be the perfect moderator bot so that a server won't need actual moderators anymore. So I want it to send a welcome message when it joins the server, but because all servers have different channel names and channels, I can't get a generic channel name to send the welcome message too.
channel = find(lambda x: x.name == 'general', guild.text_channels)
if channel and channel.permissions_for(guild.me).send_messages:
await channel.send(embed=embedvar)
This is what I have right now and as you can see it finds a channel named general and sends the welcome embed message to the general channel. But since not every server has a general channel, I want to have the bot find the 1st channel where it has permission to send messages. Is there a way to do that?
Thanks!
You can get the first channel of the guild with this way:
channel = client.get_guild(guild id).text_channels[0]
So with this code, you can do something like that:
#client.event
async def on_guild_join(guild):
channel = guild.text_channels[0]
embed = discord.Embed(title=guild.name, description="Hello, I'm here")
await channel.send(embed=embed)

Listening to reaction or message after sending embed in discord.py

I have a command, that should setup the verification process, i want that my bot, after sending an embed, listens for the next message, sent in chat, and storing that message in a variable. I know that on_message() exists, but how do to that without it?
The command should look something like this:
#client.command()
async def verificationSetup(ctx, channel_id, message):
if channel_id is not None:
embed=discord.Embed(title="Send the emoji you want to set!", description=" ")
await ctx.channel.send(embed=embed)
emoji = listen_to_message()
Solved
What I was looking for was wait_for()
Solved thanks to a guy on discord.py's discord

How to copy message from channel to channel in discord?

I'm trying to program a bot that can copy a message from one channel to another from any channel in discord, but the code that I typed in was not working even though I'm sure I followed the documentations correctly (hopefully).
#bot.command() #Moves a message from channel to channel
async def copymessage(ctx, message_id, channel_id):
"""
Copy a message from channel to channel
"""
guild = ctx.guild
channel = guild.get_channel(int(channel_id))
message = guild.fetch_message(int(message_id))
print(f'Copying {message_id} to {channel_id}')
await channel.send(message)
I have tried using a similar code (shown below) to test if I followed correctly. This code ran successfully and did what it was supposed to do.
channel = guild.get_channel(623681100778176513)
await channel.send("Sparkle is online!")
Maybe there's someone that can help me figure out the problem in my code?
fetch_message is an attribute of Messageables (guilds are not messageable), so you need to use the channel.fetch_message. It also must be awaited.
message = await channel.fetch_message(int(message_id))

Categories

Resources