so, im coding my first discord bot, and i want it to do something similar to the bot Xenon where it deletes all the channels it can, but i have no idea how to do so. this is what i have so far
#client.event
async def on_message(message):
if message.content == "delete channels":
await GuildChannel.Delete
You need to get the Guild channel from the message object
ie:
channel = message.channel # The channel the message was sent
await channel.delete()
so with your code:
#client.event
async def on_message(message):
if message.content == "delete channels":
channel = message.channel
await channel.delete()
Related
import discord
import os
client = discord.Client(intents=discord.Intents.default())
#client.event
async def on_ready():
print("We have logged in as {0.user}".format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
channel = message.channel
await channel.send('Hello!')
client.run(os.getenv('TOKEN'))
I tried to make a discord bot using discord.py. The bot comes online and everything but does not respond to my messages. Can you tell me what is wrong?
You seem to have an indentation error:
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
channel = message.channel
await channel.send('Hello!')
The last if-statement is never going to be executed. Instead, move it one indentation back such that:
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
channel = message.channel
await channel.send('Hello!')
so, Ive been doing this:
from discord.ext import commands
from discord.utils import get
client = commands.Bot(command_prefix='><')
#client.event
async def on_ready():
print("I am ready Winson or not Winson :D")
#client.event
async def on_member_join(member):
channel = client.get_channel(744440768667844698)
message = await channel.send(f"Welcome to HaveNoFaith {member}, happy to be friends with you")
#client.command()
async def ping(ctx):
await ctx.send(f"Your Ping is {round(client.latency *1000)}ms")
#client.command()
async def Help(ctx2):
await ctx2.send("Hi, Im WelcomeBot v1.0...\n\nPrefix: ><\n\nCommands: ping\n help")
#and then Im trying to do like at the message "Welcome etc" if they react with the "check reaction" in that message, they will get a role in the discord server...
you can make a command named addrr(add reaction role), which will look like this -
#client.command()
#commands.guild_only()
#commands.has_permissions(administrator=True)
async def addrr(self, ctx, channel: discord.TextChannel, message: discord.Message, emoji: discord.Emoji,
role: discord.Role):
await ctx.send(f"Setting up the reaction roles in {channel.mention}.")
await message.add_reaction(emoji)
def check1(reaction, user):
return user.id is not self.client.user.id and str(reaction.emoji) in [f"{emoji}"]
while True:
try:
reaction, user = await self.client.wait_for("reaction_add", check=check1)
if str(reaction.emoji) == f"{emoji}":
await user.add_roles(role)
await message.remove_reaction(reaction, user)
else:
await message.remove_reaction(reaction, user)
except:
await message.delete()
break
It will work like this -
><addrr <#channel mention> <message ID> <Emoji> <#Role Mention>
So you can add a reaction to the messages that was sended and use wait_for to wait for a reaction on that message. I recommend you to add the timeout. If you dont want to have this timeout, simply just send these message, save it into a list and in the raw_reaction_add event check if the emoji is the one and the message is one of the messages in your list
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)
I am trying to make a fun bot for just me and my friends. I want to have a command that says what the authors username is, with or without the tag. I tried looking up how to do this, but none worked with the way my code is currently set up.
import discord
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$WhoAmI'):
##gets author.
await message.channel.send('You are', username)
client.run('token')
I hope this makes sense, all of the code i have seen is using the ctx or #client.command
The following works on discord.py v1.3.3
message.channel.send isn't like print, it doesn't accept multiple arguments and create a string from it. Use str.format to create one string and send that back to the channel.
import discord
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$WhoAmI'):
await message.channel.send('You are {}'.format(message.author.name))
client.run('token')
Or you can just:
import discord
from discord import commands
client = commands.Bot(case_insensitive=True, command_prefix='$')
#client.command()
async def whoAmI(ctx):
await ctx.send(f'You are {ctx.message.author}')
If you want to ping the user:
import discord
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$WhoAmI'):
await message.channel.send('You are', message.author.mention)
client.run('token')
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