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!
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!
When my bot is very busy, it sometimes takes a few seconds to respond to the slash commands. However, before he can reply, Discord sends the message "The application did not respond". How can I make Discord wait longer for a message from the bot?
Have you tried using Interaction.defer()? Here's a quick example on how to use that:
#bot_instance.slash_command(name="hi")
async def hi(ctx):
await ctx.defer()
# fairly long task?
await ctx.followup.send( # Whatever you want to send...
For more information check out the API Reference: https://docs.pycord.dev/en/master/api.html#discord.Interaction
Also see a GitHub issue related to this question: https://github.com/Pycord-Development/pycord/issues/264
There is something called defer, you can do that to increase waiting time of the function
#bot_instance.command(description="Description of the command"
async def command(ctx:discord.Interaction,keyword:str)#Keyword if any
await ctx.response.defer(ephemeral=True)#This line defers the function
#Do your process here
await ctx.followup("Followup message")#This is the confirmation message
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'm trying to figure out how to write a simple bot for Discord.
However, the problem I'm facing is that for one particular server (or guild as it is called), I don't get any messages.
I am logged in this guild in the webinterface with the same user. And I see messages coming in.
However, in the client I do not see anything being logged.
The code is very simple:
import discord
class DiscordClient(discord.Client):
async def on_ready(self, *args, **kwargs):
print(f"Connected as {self.user.display_name} to:")
for guild in self.guilds:
print(f"- {guild.name}")
async def on_message(self, message):
print(f"Message from '{message.guild.name}'")
DiscordClient().run(TOKEN, bot=False)
In the on_ready, I can see it knows the guild and even the channels. So that's fine. There is a connection...
The server is the "Among Us" server which has 500k users. So maybe it's a limitation because too much users? As the on_message does trigger correctly for servers with much more limited amount of users (like 20k or so).
What setting should I adjust to connect to this server in order to receive the messages?
Thanks!
I don't see any issue with your code tbh, maybe it's an intents issue (?), try enabling some basic intents
intents = discord.Intents.default()
DiscordClient(intents=intents).run(TOKEN, bot=False)