-1

It only seems like the ban command is not working here. The "hello" and "insult me" responses work and it prints out the bot name and server name when running. Full main.py:

import os
import random
import discord
from dotenv import load_dotenv
from discord.ext import commands

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')
PREFIX = os.getenv('DISCORD_PREFIX')

client = discord.Client()
bot = commands.Bot(command_prefix='!',
                    help_command=None) # help_command to disable the default one created by this library.




@client.event
async def on_ready():
    for guild in client.guilds:
        if guild.name == GUILD:
            break
    print(
        f'{client.user} is connected to the following guild:\n'
        f'{guild.name}(id: {guild.id})'
    )


@client.event
async def on_member_join(member):
    await member.create_dm()
    await member.dm_channel.send(
        f'Hi {member.name}, welcome to my Discord server!'
    )



@client.event
async def on_message(message):
    reponse = ''
    if message.author == client.user:
        return
    greeting = [
        f"Hello there, {message.author.mention}",
        f"hi!!",
        'Penis enlargement pills are on their way!',
        f"You're stupid, {message.author.mention}",
    ]
    insult = [
        "You're a dumb dumb!",
        "You stupid meanie face!",
        "Go eat a dick, you stupid mudblood!"
    ]
    if "hello" in message.content:
        response = random.choice(greeting)
        await message.channel.send(response)
    if 'insult me' in message.content:
        response = random.choice(insult)
        await message.channel.send(response)


#bans a user with a reason
@bot.command()
@commands.has_any_role("Keyblade Master","Foretellers")
async def ban (ctx, member:discord.User=None, reason =None):
    if member == None or member == ctx.message.author:
        await ctx.channel.send("You cannot ban yourself")
        return
    if reason == None:
        reason = "For being a jerk!"
    message = f"You have been banned from {ctx.guild.name} for {reason}"
    await member.send(message)
    # await ctx.guild.ban(member, reason=reason)
    await ctx.channel.send(f"{member} is banned!")


client.run(TOKEN)

I believe the most relevant parts are:

client = discord.Client()
bot = commands.Bot(command_prefix='$',
                    help_command=None) # help_command to disable the default one created by this library.

and

#bans a user with a reason
@bot.command()
async def ban (ctx, member:discord.User=None, reason =None):
    if member == None or member == ctx.message.author:
        await ctx.channel.send("You cannot ban yourself")
        return
    if reason == None:
        reason = "For being a jerk!"
    message = f"You have been banned from {ctx.guild.name} for {reason}"
    await member.send(message)
    # await ctx.guild.ban(member, reason=reason)
    await ctx.channel.send(f"{member} is banned!")

I'm sure that i'm just incorrectly implementing the bot/command in the code somewhere, but I'm not sure where. I don't get any errors at all when I try to either run the bot or type the command in chat.

Hirschy
  • 21
  • 5
  • 3
    Does this answer your question? [Why does on\_message stop commands from working?](https://stackoverflow.com/questions/49331096/why-does-on-message-stop-commands-from-working) – TheFungusAmongUs May 19 '22 at 02:37

2 Answers2

0

You make a client and bot but you only use client for your code. Commands registered in the bot never get called because it is not even running.

You should look at tutorials and ask for help in the discord.py Discord server

Toby Cm
  • 136
  • 6
-1

I am not sure but you need to return the reason if there is no reasons typed and reason = reason is better. So your code should look like something like this:

async def ban (ctx, member:discord.User=None, reason = reason):
    if member == None or member == ctx.message.author:
        await ctx.channel.send("You cannot ban yourself")
        return
    if reason == None:
        reason = "For being a jerk!"
        return
    message = f"You have been banned from {ctx.guild.name} for {reason}"
    await member.send(message)
    # await ctx.guild.ban(member, reason=reason)
    await ctx.channel.send(f"{member} is banned!")
Adam
  • 1
  • 1
  • 3