I'm setting up a discord bot, and I want to have the bot send a confirmation or error message after the commands "ban" and "kick", can anyone help?
I have tried creating another separate command with the same arguments, except just having it send the expected message.
#client.event
async def on_ready():
print('Bot is ready!')
#client.event
async def on_member_join(ctx, member):
print(f'{member} has joined {ctx.guild.name}.')
#client.event
async def on_member_remove(ctx, member):
print(f'{member} has left {ctx.guild.name}.')
#client.event
async def on_member_join(member):
role = discord.utils.get(member.server.roles, name="Member")
await client.add_roles(member, role)
#client.command()
async def ping(ctx):
await ctx.send(f'Pong! :ping_pong: **{round(client.latency * 1000)}ms** ')
#client.command()
async def purge(ctx, amount):
await ctx.channel.purge(limit=amount)
await ctx.send(f'{member.display_name} has been kicked.')
#client.command()
async def kick(ctx, member : discord.Member, *, reason=None):
await member.kick(reason=reason)
await ctx.send(f'{member.display_name} has been kicked.'')
#client.command()
async def ban(ctx, member : discord.Member, *, reason=None):
await member.ban(reason=reason)
await ctx.send(f'The Ban Hammer has Spoken! {member.display_name} has been banned!')
#client.command()
async def pardon(ctx, *, member):
banned_users = await ctx.guild.bans()
member_name, member_discriminator = member.split('#')
for ban_entry in banned_users:
user = ban_entry.banned_users
if (user.name, user.discriminator) == (member_name, member_discriminator):
await ctx.guild.unban(user)
await ctx.send(f'Unbanned {member.display_name}')
In actuality, I think it would be great, because they clash with each other and probably will send a message, but it went wrong.
At the end of the kick command delete that last '. And also do {member.name} or {member}
Related
Oh and another problem it has is that it doesnt print the user_message in console as it should in line 12
client = discord.Client(command_prefix='',intents=discord.Intents.default())
#client.event
async def on_ready():
print('logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
username = str(message.author).split('#')[0]
user_message = str(message.content)
channel = str(message.channel.name)
print(f'{username}: {user_message} ({channel})')
if user_message == 'Hello':
await message.channel.send('Howdy!')
Any help would be appreciated!
Your indentation is all messed up here; starting with your second client.event, your code is indented way too far; here's how the code should look:
client = discord.Client(command_prefix='',intents=discord.Intents.default())
#client.event
async def on_ready():
print('logged in as {0.user}'.format(client))
# Indentation fixed here
#client.event
async def on_message(message):
username = str(message.author).split('#')[0]
user_message = str(message.content)
channel = str(message.channel.name)
print(f'{username}: {user_message} ({channel})')
if user_message == 'Hello':
await message.channel.send('Howdy!')
I just switched to Discord.py v2 after switching using nextcord for 1 or 2 months.
Im having a issue where no commands work
like no commands at all
i changed everything from "nextcord" to "discord" and even changed the setups in the cogs to the new async versions
events work, but commands themselves dont
i tried everything i knew but still didnt fix it
i tried looking in the discord doc but still couldnt find a soloution
my current code:
import discord
from discord.ext import commands
from discord.ext.commands.core import has_permissions
import asyncio
import os
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix='+', intents=intents)
##COGS
#bot.event
async def on_ready():
await asyncio.sleep(1)
print(f'We have logged in as {bot.user}')
await bot.change_presence(activity=discord.Game(name="in the Mountains"))
#bot.command()
#has_permissions(administrator = True)
async def load(ctx, arg, extension):
if ctx.author.id == 498839148904579092:
bot.load_extension(f"{arg}.{extension}")
embed = discord.Embed(description=f"{extension} module loaded!", colour=discord.Colour.dark_green())
await ctx.send(embed = embed)
print(f"{extension} loaded")
#bot.command()
#has_permissions(administrator = True)
async def unload(ctx, arg, extension):
if ctx.author.id == 498839148904579092:
bot.unload_extension(f"{arg}.{extension}")
embed = discord.Embed(description=f"{extension} module unloaded!", colour=discord.Colour.dark_green())
await ctx.send(embed = embed)
print(f"{extension} unloaded")
#bot.command()
#has_permissions(administrator = True)
async def reload(ctx, arg, extension):
if ctx.author.id == 498839148904579092:
bot.reload_extension(f"{arg}.{extension}")
embed = discord.Embed(description=f"{extension} module reloaded!", colour=discord.Colour.dark_green())
await ctx.send(embed = embed)
print(f"{extension} reloaded")
#load.error
async def alreadyloaded(ctx, error):
if isinstance(error, commands.CommandInvokeError):
embed = discord.Embed(colour=discord.Colour.dark_red())
embed.set_author(icon_url="https://cdn.discordapp.com/attachments/883349890921545748/927946906271887430/W0019f8UlvqD3dDC5.png", name="Command Invoke Error! [LOAD]")
embed.set_thumbnail(url="https://cdn.discordapp.com/attachments/883349890921545748/927946906271887430/W0019f8UlvqD3dDC5.png")
embed.add_field(name="commands.CommandInvokeError", value=error, inline=True)
embed.add_field(name="Maybe...", value="The Extension is Non-Exsistant or cannot be Loaded", inline=True)
embed.set_footer(text="get a list of Modules with [+modules]!")
await ctx.send(embed = embed, delete_after = 10)
elif isinstance(error, commands.MissingPermissions):
await ctx.message.delete()
await ctx.send(f"{ctx.author.mention}, that Command is Admin only!", delete_after = 3)
return
#unload.error
async def alreadyunloaded(ctx, error):
if isinstance(error, commands.CommandInvokeError):
embed = discord.Embed(colour=discord.Colour.dark_red())
embed.set_author(icon_url="https://cdn.discordapp.com/attachments/883349890921545748/927946906271887430/W0019f8UlvqD3dDC5.png", name="Command Invoke Error! [UNLOAD]")
embed.set_thumbnail(url="https://cdn.discordapp.com/attachments/883349890921545748/927946906271887430/W0019f8UlvqD3dDC5.png")
embed.add_field(name="commands.CommandInvokeError", value=error, inline=True)
embed.add_field(name="Maybe...", value="The Extension is Non-Exsistant or cannot be Unloaded", inline=True)
embed.set_footer(text="get a list of Modules with [+modules]!")
await ctx.send(embed = embed, delete_after = 10)
return
elif isinstance(error, commands.MissingPermissions):
await ctx.message.delete()
await ctx.send(f"{ctx.author.mention}, that Command is Admin only!", delete_after = 3)
return
#bot.command()
async def online(ctx):
print("1")
await ctx.channel.send("online")
print("hello")
async def main():
async with bot:
#await load_extensions()
await bot.start("bot auth code")
asyncio.run(main())
Discord.py v2.0 has migrated to api v10, and with this has come the introduction of Intents.message_content, which is disabled by default.
This means that you will need to explicitly enable it in code and in the panel, at https://discord.com/developers/applications.
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
bot = commands.Bot(command_prefix='+', intents=intents)
Hi i am making a Discord bot and i wanted to add a fun command that asks a emoji about a film and it wants you to answer properly. How am i suppossed to do that? Btw here is my code;
#client.command()
async def emoji(ctx):
filmemojis = [':woman_frowning: :sparkler: :woman_with_veil: :high_heel:',
':ocean: :fish: :mag:',
':nerd: :man_mage: :sparkler: :school:',
':tiger2: :person_wearing_turban: :man_rowing_boat:',
':ring: :crown: :volcano:',
':earth_americas: :rocket: :monkey_face:',
':rocket: :alien: :sunglasses: :sunglasses:',
':mouse2: :pizza: :turtle: :turtle: :turtle: :turtle:',
':mushroom: :grinning: :weary: :smirk: :triumph: :open_mouth: :innocent: :pensive:',
':ship: :couple_with_heart_woman_man:',
':blue_car: :watch: :arrow_right_hook: :hourglass_flowing_sand:']
await ctx.send(f'{random.choice(filmemojis)}\nWhat film is this?')
You are looking for Bot.wait_for
#client.command()
async def emoji(ctx):
filmemojis = [
':woman_frowning: :sparkler: :woman_with_veil: :high_heel:',
':ocean: :fish: :mag:',
':nerd: :man_mage: :sparkler: :school:',
':tiger2: :person_wearing_turban: :man_rowing_boat:',
':ring: :crown: :volcano:',
':earth_americas: :rocket: :monkey_face:',
':rocket: :alien: :sunglasses: :sunglasses:',
':mouse2: :pizza: :turtle: :turtle: :turtle: :turtle:',
':mushroom: :grinning: :weary: :smirk: :triumph: :open_mouth: :innocent: :pensive:',
':ship: :couple_with_heart_woman_man:',
':blue_car: :watch: :arrow_right_hook: :hourglass_flowing_sand:'
]
await ctx.send(f'{random.choice(filmemojis)}\nWhat film is this?')
def check(m):
"""Checks if the user that replied is the same as the one that invoked the command"""
return m.author == ctx.author
message = await client.wait_for('message', check=check) # You can also add a timeout
Adding a timeout
try:
message = await client.wait_for('message', check-check, timeout=60.0) # Change the timeout accordingly
except asyncio.TimeoutError: # You need to import asyncio for this
await ctx.send('whatever')
I want my bot when someone says something the bot to say the author's name without hashtag and also the content of the message.
But this embedVar = discord.Embed(title='The', (message.author), 'posted', description=message.content, color=0x0000FF) doesn't work.
Can you help me please
#client.event
async def on_message(message):
if str(message.channel) in ['general']:
if not message.author == client.user:
embedVar = discord.Embed(title='The', (message.author), 'posted', description=message.content, color=0x0000FF)
embedVar.set_author(name=message.author)
embedVar.set_footer(text='Facebook-')
await message.delete()
await message.channel.send(embed=embedVar)```
You can do:
async def on_message(message):
if not message.author == client.user:
await message.channel.send(message.content)
await client.delete_message(message)
Im having this issue and i still cant find a solution this is the code
#commands.Cog.listener()
async def on_raw_reaction_add(self, payload):
if payload.message_id == commands.reaction_message.id and commands.reaction_role != None:
await payload.member.add_roles(commands.reaction_role)
#commands.Cog.listener()
async def on_raw_reaction_remove(self, payload):
if payload.message_id == commands.reaction_message.id and commands.reaction_role != None:
guild = self.client.get_guild(payload.guild_id)
member = guild.get_member(payload.user_id)
await member.remove_roles(commands.reaction_role)
#commands.command()
async def set_reaction_message(self, ctx, message_id=None, role_id=None):
for channel in ctx.guild.channels:
try:
commands.reaction_message = await channel.fetch_message(int(message_id))
break
except:
pass
problem: File "c:\Users\MY-NAME\Desktop\Overige en school\Discord Bot\cogs\reaction.py", line 17, in on_raw_reaction_add
await payload.member.add_roles(commands.reaction_role)
AttributeError: 'RawReactionActionEvent' object has no attribute 'member'
can someone please help me this is been bothering me now for 2days and i can't even find a solution
I know according to the documentation discord.RawReactionActionEvent payload has the object member. But why don't you make it easier for yourself and get the member in the same way as in your on_raw_reaction_remove event? The following should work.
#commands.Cog.listener()
async def on_raw_reaction_add(self, payload):
if payload.message_id == commands.reaction_message.id and commands.reaction_role != None:
guild = self.client.get_guild(payload.guild_id)
member = guild.get_member(payload.user_id)
await member.add_roles(commands.reaction_role)
#commands.Cog.listener()
async def on_raw_reaction_remove(self, payload):
if payload.message_id == commands.reaction_message.id and commands.reaction_role != None:
guild = self.client.get_guild(payload.guild_id)
member = guild.get_member(payload.user_id)
await member.remove_roles(commands.reaction_role)
#commands.command()
async def set_reaction_message(self, ctx, message_id=None, role_id=None):
for channel in ctx.guild.channels:
try:
commands.reaction_message = await channel.fetch_message(int(message_id))
break
except:
pass