How do I create commands in Discord? - python-3.x

import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='<', intents=None)
#bot.command()
async def normal(ctx):
await ctx.send("The normal command")
bot.run("MTA1MjU0ODMzNTY4NjQwMjEwOQ.G0umvR.iElF5nzDjjJcvIvKfZQa0Lfelglh7sXPapuFHc")
When I type "<normal" it does not show me any result as if the bot is not working but ERORR does not appear
1
2
What can I do?

You forgot to run a bot. Just add at the end of the code bot.run("TOKEN"). Where "TOKEN" is a token bot that can be reached here (I think you already know exactly where the button is to get token)
Code:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='<', intents=None)
#bot.command()
async def normal(ctx):
await ctx.send("The normal command")
TOKEN = 'OTM4NTgwOTczMDU3MDkzNzAy.GnG96L.afrgSpK7OowwxQkkTnR3Sx9p76rbdRetHIChN0'# This token is not true, it's just a set of random characters
bot.run(TOKEN)

Related

How do i make a discord bot receive text after a command

I want to become a fake discord bot, so i can use: !!send or something like that.
I have the token ready and i tried some repl.it templates but i have no idea of what to do.
here is the code i tried:
import discord
import os
import time
import discord.ext
from discord.utils import get
from discord.ext import commands, tasks
from discord.ext.commands import has_permissions, CheckFailure, check
os.environ["TOKEN"] = "no-token-for-you-to-see-here"
#^ basic imports for other features of discord.py and python ^
client = discord.Client()
client = commands.Bot(command_prefix = '!!') #put your own prefix here
#client.event
async def on_ready():
print("bot online") #will print "bot online" in the console when the bot is online
#client.command(pass_context=True)
async def send(ctx,*,message):
await client.say(message)
client.run(os.getenv("TOKEN")) #get your bot token and create a key named `TOKEN` to the secrets panel then paste your bot token as the value.
#to keep your bot from shutting down use https://uptimerobot.com then create a https:// monitor and put the link to the website that appewars when you run this repl in the monitor and it will keep your bot alive by pinging the flask server
#enjoy!
It was online, but the command didn't work.
The command send should be
#client.command()
async def send(ctx, *, message: str):
await ctx.send(message)
Also, you're defining two client(s). You need only one.
client = commands.Bot(command_prefix = '!!')
You're also importing some useless modules that you don't need right now.
Your final code should look like this:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="!!")
#client.event
async def on_ready():
print("The bot is online")
#client.command()
async def send(ctx, *, message: str):
await ctx.send(message)
client.run("token-of-the-bot")
Please tell me if it works or not. Have a great day :)

I got an error on programming a discord bot

I was programing a python discord bot. I got this error:
Code:
# jokeybotmain.py
import os
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('ODg0NjkxODkzNTk2ODU2MzQw.YTcLiA.pKu1RIyV2HOTFhLOWQX0ryliyco')
client = discord.Client()
#client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
client.run(TOKEN)
Error
Your problem is that you put the token (copied from discord developer portal) as a parameter for os.getenv.
You can instead remove the part about dotenv and make your bot work, like this:
import discord
TOKEN = "Your token here" # Remember to delete it before sending your code
# Use Bot instead of Client
# https://stackoverflow.com/questions/51234778/what-are-the-differences-between-bot-and-client
bot = discord.Bot(prefixes=['!'], intents=discord.Intents.all())
Bot.run(TOKEN)
import discord
from discord.ext import commands
TOKEN = "YOUR_TOKEN"
client = commands.Bot(command_prefix="!")
#client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
client.run(TOKEN)

I am working on a bot that can mute some with a code, but when i do the command it doesn't work

So I have been working on a discord bot, that mutes people. now I've got everything but, it doesn't say anything, doesn't mute them, please help
import discord
import os
from keep_alive import keep_alive
from discord.ext import commands
bot = commands.Bot(command_prefix="~")
client = discord.Client()
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#bot.command(aliases=['m'])
#commands.has_permissions(kick_members=True)
async def mute(ctx,member : discord.Member):
muted_role = ctx.guild.get_role=807271372035850320
await member.add_roles(muted_role)
await ctx.send(member.mention + " has been muted")
keep_alive()
client.run(os.getenv('token'))
Since you have
client = discord.Client()
All your commands should begin with
#client.command()
Otherwise change it to:
bot = discord.Bot()
This should work too

discord.py send pm on member join

I'm trying to write a bot that sends a user a pm when he joins the server.
I searched and I found that to d this you need to have intents enabled, but when I enable them it gives me an error "RuntimeError: Event loop is closed". I can't figure out what exactly not working, I've seen somewhere that regenerate the bot token helps but it didn't help for me.
this is the code:
import discord
from discord.ext import commands
from dotenv import load_dotenv
from pathlib import Path
import os
# Load token
env_path = os.path.join(Path('.'), '.env')
load_dotenv(dotenv_path=env_path)
TOKEN = os.environ.get('DISCORD_TOKEN')
intents = discord.Intents(members=True)
bot = commands.Bot(command_prefix='.', intents=intents)
#bot.event
async def on_ready():
print('Bot is running...')
print(f"Name: {bot.user.name}")
print(f"ID: {bot.user.id}")
print('------')
#bot.event
async def on_member_join(member):
welcome_msg = f"hi {member}"
await member.send(welcome_msg)
bot.run(TOKEN)
I see that you are trying to DM the user as soon as they join a server, you should did {member} and the argument "member" is just a guild version of The user, and to display the name with member.name there for it will show the Member's name on the message.
Here is your Fixed Code:
import discord
from discord.ext import commands, tasks # add tasks if you're going to do tasks.
from dotenv import load_dotenv
from pathlib import Path
import os
# Load token
env_path = os.path.join(Path('.'), '.env')
load_dotenv(dotenv_path=env_path)
TOKEN = os.environ.get('DISCORD_TOKEN')
intents = discord.Intents(members=True)
bot = commands.Bot(command_prefix='.', intents=intents)
#bot.event
async def on_ready():
print('Bot is running...')
print(f"Name: {bot.user.name}")
print(f"ID: {bot.user.id}")
print('------')
#bot.event
async def on_member_join(member):
welcome_msg = f"hi {member.name}" # You don't need to define the message.
await member.send(welcome_msg) # You can do await member.send("message") instead of defining it.
bot.run(TOKEN)
Hope this helped. Have a nice day!

discord bot only seeing itself with command

import discord
import random
from discord.ext import commands, tasks
from itertools import cycle
from random import choice
client = commands.Bot(command_prefix = '.')
intents = discord.Intents.all()
#client.command()
#commands.guild_only()
async def ruser(ctx):
await ctx.send(choice(ctx.guild.members))
This is some code for a discord bot I'm working on. If you type ".ruser", it's supposed to send a message saying the user. The problem is that it is only returning the bots name, and not any of the users.
You didn't actually pass in the intents, you only declared the variable. You're supposed to add them to your bot as well.
intents = discord.Intents.all()
client = commands.Bot(command_prefix='.', intents=intents)

Resources