So I want to know if it is possible, that a bot gets the content sent to it in a dm and send that in a specifyed channel on a server.
So basically you dm the bot the word "test" and the bots sends the word in a channel of a server
Yes, it is possible for a bot to receive a direct message and then repost the message in a specified channel on a server. This can be done using the Discord API.
You can do the following:
Create a Discord bot and add it to your server. You can do this using the Discord developer portal.
Use the Discord API to listen for messages sent to the bot in a DM. You can do this using the message event and the DMChannel class in the Discord API.
When the bot receives a DM, use the Discord API to repost the message in the specified channel on the server. You can do this using the send method of the TextChannel class in the Discord API.
Yes, this is possible:
First, we use the on_message() event to detect when a message is sent.
We then check if the message is sent in a DM.
If so, we will send a message to a specific channel.
Here is one way you can implement it:
# import ...
bot = discord.Bot(...)
#bot.event
async def on_message(message):
# check if it's
if isinstance(message.channel, discord.DMChannel):
# get the channel from ID
channel = client.get_channel(CHANNEL_ID) # put channel ID here
await channel.send("test") # whatever you want to send
#bot.event:
async on_message(message):
if message.DMChannel:
# define channel and send message to channel.
You can also refer to DOCS.
Related
this is my first time using python and I've been trying to make a selfbot that would send a slash command to a specific guild channel on startup with discord py for like 5 hours now and I have no idea what I'm doing, does anybody know how to specify a server and channel when sending a message/command or a reliable way to send slash commands that isn't reliant on a discontinued library that doesn't work anymore
I tried going through threads on here aswell as the discord py library to figure out how to do it but I haven't found anything at all
You can get the ID of a channel if you enable Developer Mode in the settings on Discord:
Then you can right click on a channel to copy its ID:
I python you can use discord.py. For example:
import discord
client = discord.Client()
#client.event
async def on_ready():
print('Logged in as', client.user)
channel = client.get_channel(ChannelID)
await channel.send('/command')
await client.close()
client.run('token')
Here you need to replace ChannelID with your channel ID and token with your Discord Bot's token.
If you want to use your account (not recommended) you can pass bot=False in client.run:
client.run('token', bot=False)
To get your account's token, you need to load Discord from a browser (I use Chrome to demonstrate);
Press F12 or Ctrl+Shift+I, to open devtools;
Switch to the Network tab;
Filter the keyword messages;
Open a channel in discord, and a request will be sent to /api/v9/channels/-channel_ID-/messages?limit=50.
Look at that request (click on it, and then switch to Headers tab);
Scroll down to Request headers, and grab your token.
I have for the moment a code that sends a message but only for my own discord server as a bot.
What I would like it's automated sending message to others servers to simulate an activity as if it were my main account
def sendMessage(ch, ms):
client = discord.Client()
#client.event
async def on_ready():
channel = client.get_channel(ch)
await channel.send(ms)
return ""
client.run('token')
How to make the sendMessage code sending a message using my main account?
Please, do not do this.
Like Silvio said, if you find a way to send messages as a user through code and you are caught then you might get banned. Don't say we didn't warn you!
I'm creating a simple Twitch bot for personal use. I'm using twitchio.ext commands. Everything works fine, I'm connected, I'm able to print all the messages from the chat, my bot responds to commands but I'm not able to send a message to chat and I don't know why.
from twitchio.ext import commands
class Bot(commands.Bot):
def __init__(self):
super().__init__(token='oauth:censored', prefix='g ', nick = "nick of the bot", irc_token = "censored", initial_channels=['channel'])
async def event_ready(self):
print(f'Using bot as {self.nick}')
#commands.command()
async def test(self, ctx: commands.Context):
await ctx.send('test')
print("printed")
bot = Bot()
#bot.event()
async def event_message(msg):
print(msg.author.name)
print(msg.content)
await bot.handle_commands(msg)
bot.run()
When I type "g test" in chat, the message "test" is not sent but the message "printed" is printed to the console. Do you know where could be the problem? Also, I would like to ask if there is any way how to send a message to chat directly without responding to a command or event.
Try adding the bot as a mod
The 'nick' in the bot init() function doesn't seem to do anything the nick is linked to your IRC username.
I'm assuming the channel in your initial_channels in the init() func isn't actually the channel you put there since you said you were able to type in the chat.
If the channel that you are using the bot from(linked to the oauth key) is not the same channel as you are connecting to in the chat then you probably need to add the bots username as a moderator in that chat
try
/mod [bot_username]
in the chat as the channel owner to do this
Also if your bot is the same account you are sending commands from on twitch the rate limit will be reached unless they are a mod. So either make a dedicated account for your bot or mod the bot on the server
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)
I have problem creating simple python script which sends message to default channel when executed from terminal.
import discord
#bot.event
async def on_ready(ctx):
print('Online')
await ctx.send('Message sent!')
bot.run('MYTOKEN')
with this example I keep getting "ctx" is not defined
The issue here is that the on_ready event should not receive any parameters. See the Minimal Bot documentation here and the on_ready documentation in the Event Reference here. If you want to send a message when the bot connects. You must first get the channel object and then use the send method. You can find an example of getting a channel and sending a message in the FAQ section of the docs here