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.