Discord bot commands execute multiple times - python

It worked at first, but then somewhy it started to give the answers 4-5 times at once, any idea why?
import discord
from discord.ext import commands
client = commands.Bot(command_prefix = '.')
#client.event
async def once_ready():
print('Bot is ready.')
#client.event
async def on_member_join(member):
print(f'{member} has joined the server.')
#client.event
async def on_member_remove(member):
print(f'{member} has left the server.')
#client.command()
async def ping(ctx):
await ctx.send('Pong!')
client.run('my token here')

So, the problem was that I couldn't shut down the already running bots in Atom.
If you add the code that I wrote down there (that only the owner can
use) will shut down the already running bots (write /shutdown in
discord server or whatever your prefix is).
However, you may need a PC restart after saving the bot with this code.
#client.command()
#commands.is_owner()
async def shutdown(ctx):
await ctx.bot.logout()
So every time if you want to edit your command, you write /shutdown
and edit it, after that, you can start it again.
I hope this works for you and that I could help if you had the same problem.

Related

Pycord bot not responding to commands

I have a discord bot with automod and XP system. I have made discord bots before so I know how to make a command, but now it does not seem to work and I don't know why.
When I say a curse it deletes my message and everything there works fine so the on_message works, but those commands don't work. It does not give any error when I type a command, but the but doesn't respond.
Any help would be appreciated.
Here is my code:
import discord
from discord.ext import commands
# Set up the client and command prefix
intents = discord.Intents.all() # Enable all intents
client = commands.Bot(prefix='$', intents=intents)
#client.event
async def on_ready():
print('Bot ready')
#client.command()
async def ping(ctx):
print("ping")
await ctx.send('pong')
#client.event
async def on_message(message):
# Ignore messages sent by the bot
if message.author == client.user:
return
# Do some XP things
...
# AutoMod things
...
# Process any commands that are sent by the user
await client.process_commands(message)
print("processing commands")
#client.command()
async def leaderboard(ctx):
# do something
...
# Start the bot
client.run('TOKEN')
Also all intents are enabled in discord developer portal
So what I expected to happen is the program would first print Bot ready. Then I would write $ping to discord and the program would print processing commands and then print pong and say pong in discord.
What actually happened is that the program Printed Bot ready. Then I wrote $ping, the program prints processing commands. And then nothing else happens

How to make Only you can see this message in Discord py?

How to make Only you can see this message in Discord py ?
i want to make discord bot reply user with 'only you can see this message' in discord.py
import discord
from discord.ext import commands
client = commands.Bot(
help_command=None,
command_prefix=">"
)
#client.event
async def on_ready():
print('Connection as {0.user}'.format(client))
#client.command()
async def Hi(ctx):
await ctx.reply('Hello')
client.run('token')
There are two ways of doing this :
One simple solution is through sending DM (direct message) to the user, here is an example :
DM = await ctx.author.create_dm()
await DM.send("Hello")
The another solution, the more complicated one is through responding to interactions with ephemeral messages, which are related to slash-commands and buttons. I do not recommend doing it this way if you are not already dealing with interactions, since you will have to implement a lot of stuff.
If you just need to display this message then
#client.command()
async def Hi(ctx):
await ctx.send('Hello')

How to stop repetitive messages, and the token is changed, but it doesn't run

I started learning python today and made a Discord bot. I have a few problems:
If message.author == was used in the on_message, but the bot continued to reply to itself.
Afterwards, a new bot was created with a new token and the code didn't work.
I searched a lot on this site and Google. I didn't find any solution. It's okay to modify my code. Everything is from the internet, so it can be a mess. Please help me.
import discord
import asyncio
from discord.ext import commands
client = discord.Client()
bot = commands.Bot(command_prefix="!")
#bot.event
async def on_ready():
print('Loggend in Bot: ', bot.user.name)
print('Bot id: ', bot.user.id)
print('connection was succesful!')
print('=' * 30)
#client.event
async def on_message(message) :
if on_message.content.startswith('!의뢰'):
msg = on_message.channel.content[3:]
embed = discord.Embed(title = "브리핑", description = msg, color = 0x62c1cc)
embed.set_thumbnail(url="https://i.imgur.com/UDJYlV3.png")
embed.set_footer(text="C0de")
await on_message.channel.send("새로운 의뢰가 들어왔습니다", embed=embed)
await client.process_commands(message)
client.run("My bot's token.")
Your code was messy, but it should work now. I included comments to let you know how everything works. I think the good starting point to making your own bot is reading documentation. Especially Quickstart that shows you a simple example with explanation.
Write !example or hello to see how it works.
import discord
import asyncio
from discord.ext import commands
# you created 'client' and 'bot' and used them randomly. Create one and use it for everything:
client = commands.Bot(command_prefix="!") # you can change commands prefix here
#client.event
async def on_ready(): # this will run everytime bot is started
print('Logged as:', client.user)
print('ID:', client.user.id)
print('=' * 30)
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('hello'): # you can change what your bot should react to
await message.channel.send("Hello! (This is not a command. It will run everytime a user sends a message and it starts with `hello`).")
await client.process_commands(message)
#client.command()
async def example(ctx): # you can run this command by sending command name and prefix before it, (e.g. !example)
await ctx.send("Hey! This is an example command.")
client.run("YOUR TOKEN HERE")

Bot stopped working even though code is fine

This is my first time coding a discord bot and I have been following tutorials mainly but I wanted to make a unique game so I tried to make it myself. But after messing around with it for a while I realized that the only part of my code that works is the mcguccy is an idiot part none of the client. Command parts work.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='!')
#client.command()
async def ping1(ctx):
await ctx.send("pong!")
#client.event
async def on_ready():
await client.change_presence(status=discord.Status.online, activity=discord.Game("cranking 90s on shitters"))
print("bot is ready!")
#client.event
async def on_member_join(member):
print(f'{member} has joined :weary:.')
#client.event
async def on_member_remove(member):
print(f'{member} has left :weary:')
#client.command()
async def helb(ctx):
await ctx.send('#everyone look at this idiot')
#client.command()
async def ping(ctx):
await ctx.send(f'here you go: {round(client.latency * 1000)}ms')
#client.command()
async def commands(ctx):
await ctx.send('1. helb it will help you. 2. ping it will tell you the bots ping. ')
#client.command()
async def overlord(ctx):
await ctx.send("muah hah hah YOUR SERVER IS MINE")
keywords = ["mcguccy is an idiot", "kick mcguccy", "i want to nuke the server"]
#client.event
async def on_message(message):
for i in range(len(keywords)):
if keywords[i] in message.content:
for j in range(20):
await message.channel.send(f"#everyone someone has spammed :weary:")
There are a few things to look at here:
Your client variable is a Bot type, not a Client type.
client = commands.Bot(command_prefix='!')
This means you need to use #bot decorators instead of #client decorators:
#client.command()
async def helb(ctx):
...
# Becomes
#bot.command()
async def helb(ctx):
...
The .run() method is never called.
Because of this, the bot's event loop doesn't start. You need to initialize the Client event loop so the bot can listen for commands to pass:
if __name__=='__main__':
client.run(TOKEN)
That code needs to be the very last statement run in the file, as it's blocking.
The .run() command requires an API token.
You can acquire said token by creating a bot account if you don't have one already. If you do have one, you'll need to pass it as the first paramater to the .run() method.

Discord.py: Why do the welcome and goodbye messages repeat?

Problem: I am trying to have it so that the bot sends a single message in general, but the messages continuously repeat, seemingly increasing each time this occurs. Also, if I switch the message to something else, the repeated parts are of the old message.
Code:
import discord
from discord.ext import commands
client=commands.Bot(command_prefix = '.')
#client.event
async def on_ready():
print('ok')
#client.event
async def on_member_join(member):
channel=discord.utils.get(member.guild.channels, name="general")
await channel.send("Hi {}".format(member))
#client.event
async def on_member_remove(member):
channel=discord.utils.get(member.guild.channels, name="general")
await channel.send("Bye")
client.run(token)
Since the code is under the function variable then you have to end each function.
It might be due to multiple instances of the application are running. Check your task manager and search for python process, End it if there are multiple and re-run the script.

Categories