Sending slash command to specific guild channel - python

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.

Related

Getting content from a dm in discord.py

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.

Discord bot won't come online unless it is Administrator

I am playing with my first discord bot, but have been unable to get it to read any messages. I looked through several tutorials, and all of them are using the Administrator privilege for convenience. However, I would like to restrict the bot's permissions, as I only want to read/send messages and maybe send some emojis.
When I select only permissions pertaining to sending texts and emojis, I noticed that the bot isn't even listed in the Online section in the Discord app on the right. HOWEVER, if I add in the Administrator permission, suddenly the bot is seen online, and will correctly read/send messages.
Here is what I have:
I have created my own server, with me as the only member and I am administrator (I have a crown next to my username in the Online section).
My Bot permissions (set through Discord's Oauth web page):
When I go to the Discord App and Server settings -> Integrations -> <role_name>, I see my bot, and it shows:
Not sure why there are different permissions set on the Role vs what I set through the Discord OAuth web page though.
The code is super simple:
import discord
client = discord.Client()
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
client.run('<key>')
On running the script, I do see the message:
We have logged in as mytest_bot#9274
So it is connected and working, but the bot just isn't online. In fact, if I add in ALL Permissions EXCEPT Administrator, the bot still WON'T come online:
It's ONLY when I set the bot to Administrator that the bot then magically comes online and starts processing the on_message() code.
What am I missing here?
Check if the bot on your server has the same name and client-id like the bot from your token in the bot file.
Try to use a command. :-
#client.command()
async def test(ctx):
await ctx.send("test")

Can't send private messages from my discord bot to user?

I want to send a private message to a specific user with my discord bot. I am using discord==1.0.1
discord.py==1.3.1. I already tried using the docs (https://discordpy.readthedocs.io/en/latest/api.html?highlight=private%20message#discord.DMChannel) but i dont understand that. I tried following code, which didnt worked:
#client.command()
async def cmds(ctx):
embed = discord.Embed(title='Discord Server Befehle',
description='Description',
color=0x374883)
[...]
msg = await ctx.send(embed=embed)
await ctx.author.create_dm('Hi')
print(f'{command_prefix}help ausgeführt von {ctx.author}')
Try using the send function instead of the create_dm. create_dm only creates the channel between two users which discord does automatically as far as i know.
according to documentation
This should be rarely called, as this is done transparently for most people.
so it should be
ctx.author.send('Hi')

Getting ping from remote server in Discord.py?

I'm writing a discord bot with discord.py for my personal server. My friend and I are frequent gamers, and love playing League of Legends, so I figured a cool functionality for our bot would be to display our current latency between us and Riot's servers. So far, I have this attached code, but this always returns the ping from where the bot is hosted. Is there a way for me to get the ping of the person who issues the command to the bot? Code uses pythonping, found here https://pypi.org/project/pythonping/. Thanks :)
import discord
from discord.ext import commands
from pythonping import ping
client = commands.Bot(command_prefix = '.')
#client.event
async def on_ready():
print('Bot is ready.')
#client.command()
async def lolping(ctx):
server = '104.160.131.3'
response = ping(server, size=40, count = 10)
print(response)
await ctx.send(f'Current NA LOL Ping: {response.rtt_avg_ms}ms')
Not possible. The reason it always returns the bot's ping is because the bot initiates the ping request. The command just tells the bot to do it.
The bot cannot start a ping request from another machine.
Adding to #Benjin's answer ...
As per the definition of a ping, the machine sending the ping (in this case the discord bot) measures the time it takes to receive its packet back. Therefore, as the bot cannot send a packet from the user's machine, it is impossible for it to measure their ping.
Perhaps you could have the bot provide a link to Riot's lag-report page here so that users can check themselves based on their locations. However, this seems to only work for certain locations and I don't know how useful it is.
Good Luck!

How to get a Discord Bot to return the formatted name of a Channel?

I'm attempting to create a Discord bot using Discord.py for a server I'm in, I'm trying to get the bot to mention (blue links) other channels in the server. I thought it would have been using something like discord.TextChannel.name, discord.TextChannel.mention. But neither of those work, they only return something along the lines of "<property object at 0x03CE8B68>".
Code:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='$')
client = discord.Client()
token = <token>
#bot.command()
async def test(ctx, arg):
await ctx.send(arg)
#bot.command()
async def mentionChan(ctx):
await ctx.send(ctx.guild.get_channel('662129928463974411').mention)
client.run(token)
#botclient.command()
async def mentionChan(ctx):
await ctx.send(ctx.guild.get_channel(CHANNEL_ID).mention)
This will mention the channel with the given id. You can get the id of a channel by right clicking a channel and selecting "Copy ID". This is only possible after you have turned on developer mode at the appearance tab in User Settings.
ctx.guild.text_channels will give you a list of all the text channels in the server if you want to use that.

Categories

Resources