ok so here is my code
import discord
from discord.ext import commands
TOKEN = 'THIS_IS_MY_BOT_TOKEN'
client = commands.Bot(command_prefix = '.')
#client.event
async def on_message(message):
# we do not want the bot to reply to itself
if message.author == client.user:
return
#typing cat
if message.content.startswith('!cat'):
msg = 'https://media.giphy.com/media/JIX9t2j0ZTN9S/giphy.gif'.format(message)
await client.send_message(message.channel, msg)
#I dont need sleep i need awnsers
if message.content.startswith('!sleep'):
msg = 'https://i.kym-cdn.com/entries/icons/original/000/030/338/New.jpg'.format(message)
await client.send_message(message.channel, msg)
#murica
if message.content.startswith('!murica'):
msg = 'https://www.dictionary.com/e/wp-content/uploads/2018/08/Murica_1000x700.jpg'.format(message)
await client.send_message(message.channel, msg)
if message.content.startswith('!why'):
msg = 'https://drive.google.com/file/d/1rb132Y785zUjj2RP2G-a_yXBcNK5Ut9z/view?usp=sharing'.format(message)
await client.send_message(message.channel, msg)
#client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
#client.command(pass_context=True)
async def join(ctx):
if ctx.message.author.voice:
channel = ctx.message.author.voice.channel
await channel.connect()
client.run(TOKEN)
the bot joins the sever, but when I say .join, nothing happens
the voice channel I want to join is called Club Meeting if that helps
Not entirely sure why, I have no errors when I run it. Anyone have any idea whats going on?
I think the problem is your lack of Bot.process_commands You need to put that at the end of your on_message function. That seems to be the reason your command isn't working.
From The Docs:
Why does on_message make my commands stop working?
https://discordpy.readthedocs.io/en/latest/faq.html#why-does-on-message-make-my-commands-stop-working
Related
Soo i want my bot to send a message when someone join but it is not workin
#client.event
async def on_member_join(member):
channel = discord.util.get(member.Guild, name='general')
await channel.send(f'Hey welcome to my server {member.mention}, hope you enjoy this server!')
and
i want my bot to take a massage from me and send it to the guy i say but this is not workin too
massage = await client.wait_for('massage', check=check)
await ctx.send(f'send massage to {member} ')
await member.send(f'{ctx.member.mention} has a massage for you: \n {massage}')
and
here is my WHOLE code:
import os, discord,asyncio
import keep_alive
from discord.ext import commands
client = commands.Bot(command_prefix="!")
token = os.environ.get('Token')
GUILD = os.environ.get('Guild')
#client.event
async def on_ready():
print(f'{client.user} is connected')
#client.command()
async def dm(ctx, member: discord.Member):
await ctx.send('what do u want to say bitch!')
def check(m):
return m.author.id == ctx.author.id
massage = await client.wait_for('massage', check=check)
await ctx.send(f'send massage to {member} ')
await member.send(f'{ctx.member.mention} has a massage for you: \n {massage}')
#client.event
async def on_member_join(member):
channel = discord.util.get(member.Guild, name='general')
await channel.send(f'Hey welcome to my server {member.mention}, hope you enjoy this server!')
keep_alive.keep_alive()
client.run(token)
For making a welcome channel, it is safer to use get_channel instead of get. Because in your code, every time you rename your channel, you need to change your code too, but channel ids cannot be changed until if you delete and create another one with the same name.
Code:
#client.event
async def on_member_join(member):
channel = client.get_channel(YOUR_CHANNEL_ID_GOES_HERE)
await channel.send(f'Hey welcome to my server {member.mention}, hope you enjoy this server!')
As for the dm command, I recommend you to get your message as a function parameter. Also you can check when you're DM'ing your bot with the isinstance() function. There is a * before the message parameter though. Its purpose is collecting all of your messages with or without spacing.
Code:
#client.command()
async def dm(ctx, member:discord.Member,*, message):
if isinstance(ctx.channel,discord.DMChannel):
await member.send(f'{ctx.member.mention} has a message for you: \n {message}')
Commands not working but events are tried overriding my on_message but that didn't work. When I comment out the second client.event and down client.command works. Any idea of what I could be doing wrong? am I missing something?
import discord
from discord.ext import commands
import random
import time
from datetime import date
client = commands.Bot(command_prefix = '.')
#client = discord.Client()
#client.event
async def on_ready():
print('we have logged in as {0.user}'.format(client))
#client.command()
async def clr(ctx, amount=5):
await ctx.channel.purge(limit=amount)
#client.command(aliases =['should', 'will'])
async def _8ball(ctx):
responses =['As i see it, yes.',
'Ask again later.',
'Better not tell you now.',
"Don't count on it",
'Yes!']
await ctx.send(random.choice(responses))
#client.event()
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('hello'):
await message.channel.send('Hello how are you?')
Based on the docs (the ones mentioned by moinierer3000 in the comments) as well as other questions on stack (listed below), on_message will stop your commands from working if you do not process the commands.
#client.event()
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('hello'):
await message.channel.send('Hello how are you?')
await client.process_commands(message)
Other questions like this:
discord.py #bot.command() not running
Discord.py Commands not working because of a on_message event
Prefixed and non prefix commands are not working together on python discord bot
How to create a Discord.py command?
In fact, it doesn't show anything in the console at all, what should I do?
There's no error, nothing is executing.
import discord
import time
from discord.ext import commands
client = commands.Bot(command_prefix='/')
# This detects when the bot started.
#client.event
async def on_ready():
print(f'{client.user.name} is ready!')
# This detects when someone sends a message.
#client.event
async def on_message(message):
if message.author == client.user:
return
if 'fuck' in message.content:
await message.delete()
async with message.channel.typing():
time.sleep(1)
await message.channel.send('You cannot swear in this discord server!')
if 'shit' in message.content:
await message.delete()
async with message.channel.typing():
time.sleep(1)
await message.channel.send('You cannot swear in this discord server!')
if 'bitch' in message.content:
await message.delete()
async with message.channel.typing():
time.sleep(1)
await message.channel.send('You cannot swear in this discord server!')
if 'https://' in message.content:
await message.delete()
async with message.channel.typing():
time.sleep(1)
await message.channel.send('You cannot send links in this discord server!')
# This cteates a command.
#commands.command()
async def test(ctx, arg):
async with message.channel.typing():
time.sleep(1)
await ctx.send(arg)
client.add_command(test)
client.run('TOKEN')
You need to add client.process_commands at the end of the on_message event.
async def on_message(message):
# ...
await client.process_commands(message)
Another thing wrong in your code is that you're not defining message in the test command, you can fix it by using the Context.message attribute
async with ctx.message.typing():
# ...
# Or
async with ctx.typing():
# ...
Also you can simply use the client.command decorator instead of commands.command so you don't need to add the line client.add_command
#client.command()
async def test(ctx, arg):
# ...
Reference:
Bot.process_commands
Context.message
Context.typing
There is a problem with my code. it does not run any commands :( I know for a fact that the problem is from the on_message part but i dont know how to fix it sadly.
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='//')
bdl = open('*Filelocation*', 'r')
badwordslist = bdl.read().split()
bdl.close()
#bot.command()
async def hello(context):
print('it works')
context.send('Hello!')
#bot.event
async def on_connect():
print('Connected.')
#bot.event
async def on_ready():
print('READY.')
#bot.event
async def on_message(message):
for badword in message.content.lower().split():
if badword in badwordslist:
await message.channel.send(f'Hey! {message.author.mention} ! Don\'t be rude!')
print(f'{message.author} Said A Bad Word.')
break
else:
return
bot.run("*Token*")
on_message event blocks other commands. If you want to prevent this, you should process commands with await bot.process_commands(message)
#bot.event
async def on_message(message):
for badword in message.content.lower().split():
if badword in badwordslist:
await message.channel.send(f'Hey! {message.author.mention} ! Don\'t be rude!')
print(f'{message.author} Said A Bad Word.')
break
await bot.process_commands(message)
so I'm working on making my own bot for my server and after a while I finally found a string for autorole & role assignment that worked.
I then kept on adding another string for the bot simply replying "Hello". As soon as I add that the role commands won't work anymore. Once I take it out it works again.
On the other hand I have a 8ball and a dice roll command that works with and without the Hello Command
I have no idea what is the problem...
#client.event
async def on_member_join(member):
channel = discord.utils.get(member.guild.channels, name='entrance')
await channel.send(f'Welcome {member.mention} to Dreamy Castle! \n Please make sure to read the rules!')
role = discord.utils.get(member.guild.roles, name="Peasants")
await member.add_roles(role)
#client.event
async def on_message(message):
if message.content.startswith('+acceptrules'):
member = message.author
role1 = discord.utils.get(member.guild.roles, name='The People')
await member.add_roles(role1)
#client.event #this is the hello command
async def on_message(message):
message.content.lower()
if message.content.startswith('Hello Conny'):
await message.channel.send('Hello!')
Use if and elif not 2 different functions for same event.
Also you might need commands.Bot for a fully functional commanded bot.
#client.event
async def on_message(message):
if message.content.startswith('+acceptrules'):
member = message.author
role1 = discord.utils.get(member.guild.roles, name='The People')
await member.add_roles(role1)
elif message.content.lower().startswith("hello conny"):
await message.channel.send("Hello!")
await client.process_commands(message)