my code:
#Ban command
#bot.command()
async def ban(ctx, member : discord.Member, *, reason='nothing'):
#to check if user has permissions
if has_permissions(ban_members=True):
#to not let admins ban each other
check = False
for i in member.roles:
if i in ctx.author.roles[1:]:
check = True
if(check):
await ctx.send('❌ Cant ban admins/moderators.')
else:
await member.ban(reason=reason)
await ctx.send(f'✓ {member} has been banned')
else:
await ctx.send(' you dont have that permission')
The problem is that users who don't have perms if used the command does not print: you don't have that permission and I get an error.
You could try ctx.author.guild_permissions. Or using discord.py's method of #bot.has_permissions(ban_members=True) just after #bot.command()
#bot.command()
async def ban(ctx, member : discord.Member, *, reason='nothing'):
#to check if user has permissions
if ctx.author.guild_permissions.ban_members:
#to not let admins ban each other
check = False
for i in member.roles:
if i in ctx.author.roles[1:]:
check = True
if(check):
await ctx.send('❌ Cant ban admins/moderators.')
else:
await member.ban(reason=reason)
await ctx.send(f'✓ {member} has been banned')
else:
await ctx.send(' you dont have that permission')
Related
I’m trying to code a simple music bot for discord in python. I tried coding a queue command so that I can queue a certain song (searching for songs and playing them etc. all work). This is my code for the queue command:
#commands.command()
async def checkqueue(self, ctx):
if len(self.song_queue[ctx.guild.id]) == 0:
return await ctx.send("Queue is empty")
else:
embed = discord.Embed(title="Song Queue", description="", colour=discord.colour.dark_gold())
i = 1
for url in self.song_queue[ctx.guild.id]:
embed.description += f"{i}) {url}\n"
i += 1
await ctx.send(embed=embed)
When the queue is empty, the bot sends a message in discord saying "Queue is empty”. When I use the play command, it adds a song to the queue. However, when I try to use the checkqueue command when there’s at least one song in the queue, the bot doesn’t send the embed. Not sure if this is a problem with the code in the queue command or outside the queue command, so here’s the cut-down version of the rest of my code in my commands Cog.
import discord
from discord.ext import commands
import youtube_dl
import pafy
import asyncio
class Commands(commands.Cog):
def __init__(self, client):
self.client = client
self.song_queue = {}
#commands.Cog.listener()
async def on_ready(self):
for guild in self.client.guilds:
self.song_queue[guild.id] = []
async def check_queue(self, ctx):
if len(self.song_queue[ctx.guild.id]) > 0:
ctx.voice_client.stop()
await self.play_song(ctx, self.song_queue[ctx.guild.id][0])
self.song_queue[ctx.guild.id].pop(0)
async def search_song(self, amount, song, get_url=False):
info = await self.client.loop.run_in_executor(None, lambda: youtube_dl.YoutubeDL({"format": "bestaudio", "quiet" : True}).extract_info(f"ytsearch{amount}:{song}", download=False, ie_key="YoutubeSearch"))
if len(info["entries"]) == 0:
return None
return [entry["webpage_url"] for entry in info["entries"]] if get_url else info
async def play_song(self, ctx, song):
url = pafy.new(song).getbestaudio().url
ctx.voice_client.play(discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(url)), after=lambda error: self.client.loop.create_task(self.check_queue(ctx)))
ctx.voice_client.source.volume = 0.5
#commands.command()
async def stop(self, ctx):
if ctx.voice_client is not None:
return await ctx.voice_client.disconnect()
return await ctx.send("Disconnected")
else:
return await ctx.send("I am not connected to a voice channel")
#commands.command()
async def join(self, ctx):
if ctx.author.voice is None:
return await ctx.send("You are not connected to a voice channel")
else:
channel = ctx.author.voice.channel
await channel.connect()
await ctx.send(f"Connected to voice channel: '{channel}'")
#commands.command()
async def play(self, ctx, *, song=None):
if song is None:
return await ctx.send("You must include a song to play.")
if ctx.voice_client is None:
return await ctx.send("I must be in a voice channel to play a song.")
if not ("youtube.com/watch?" in song or "https://youtu.be/" in song):
await ctx.send("Searching for a song, this may take a few seconds...")
result = await self.search_song(1, song, get_url=True)
if result is None:
return await ctx.send("Sorry, I couldn't find the song you asked for. Try using my search command to find the song you want.")
song = result[0]
if ctx.voice_client.source is not None:
queue_len = len(self.song_queue[ctx.guild.id])
if queue_len < 10:
self.song_queue[ctx.guild.id].append(song)
return await ctx.send(f"Song added to the queue at position {queue_len+1}")
else:
return await ctx.send("Maximum queue limit has been reached, please wait for the current song to end to add more songs to the queue")
await self.play_song(ctx, song)
await ctx.send(f"Now playing: {song}")
#commands.command()
async def search(self, ctx, *, song=None):
if song is None:
return await ctx.send("Please include a song to search for")
await ctx.send("Searching for song, this may take a few seconds...")
info = await self.search_song(5, song)
embed = discord.Embed(title=f"Results for '{song}':", description="You can use these URL's to play the song\n", colour=discord.Colour.blue())
amount = 0
for entry in info["entries"]:
embed.description += f"[{entry['title']}]({entry['webpage_url']})\n"
amount += 1
embed.set_footer(text=f"Displaying the first {amount} results.")
await ctx.send(embed=embed)
#commands.command()
async def checkqueue(self, ctx):
if len(self.song_queue[ctx.guild.id]) == 0:
return await ctx.send("Queue is empty")
else:
embed = discord.Embed(title="Song Queue", description="", colour=discord.colour.blue())
i = 1
for url in self.song_queue[ctx.guild.id]:
embed.description += f"{i}) {url}\n"
i += 1
await ctx.send(embed=embed)
#commands.command()
async def queue(self, ctx, *, song=None):
if song is None:
return await ctx.send("You must include a song to queue.")
if not ("youtube.com/watch?" in song or "https://youtu.be/" in song):
await ctx.send("Searching for a song, this may take a few seconds...")
result = await self.search_song(1, song, get_url=True)
if result is None:
return await ctx.send("Sorry, I couldn't find the song you asked for. Try using my search command to find the song you want.")
song = result[0]
if ctx.voice_client.source is not None:
queue_len = len(self.song_queue[ctx.guild.id])
if queue_len < 10:
self.song_queue[ctx.guild.id].append(song)
return await ctx.send(f"Song added to the queue at position {queue_len+1}")
else:
return await ctx.send("Maximum queue limit has been reached, please wait for the current song to end to add more songs to the queue")
The issue that I was having with your code was the color of the Embed. Everything else went smoothly when I ran your code. When you referenced the Colour, you didn't properly access the function.
What you did:
embed = discord.Embed(title="Song Queue",
description="",
colour=discord.colour.dark_gold())
What you should do:
embed = discord.Embed(title="Song Queue",
description="",
colour=discord.Colour.dark_gold())
I have made a discord music bot using python, lavalink and wavelink. I have made it according to a tutorial where it plays the songs I select from a list of search results. I want to make the bot play the first search result directly instead of me having to select from the list? I have tried to check other tutorials and codes but implementing them doesn't seem to work as their full codes for the classes or they involve ffmpeg or ytdll.
async def add_tracks(self,ctx, tracks):
if not tracks:
raise NoTracksFound #Goes to a pass command
if isinstance(tracks,wavelink.TrackPlaylist):
self.queue.add(*tracks.tracks)
elif len(tracks) == 1:
self.queue.add(tracks[0])
await ctx.send(f"{tracks[0].title} added to list")
else:
if (track := await self.choose_track(ctx, tracks)) is not None:
self.queue.add(track)
await ctx.send(f"Added {track.title} to list")
if not self.is_playing:
await self.start_playback()
async def choose_track(self, ctx, tracks):
def _check(r, u):
return (
r.emoji in OPTIONS.keys()
and u == ctx.author
and r.message.id == msg.id
)
embed = discord.Embed(
title="Choose a song",
description=(
"\n".join(
f"**{i+1}.** {t.title} ({t.length//60000}:{str(t.length%60).zfill(2)})"
for i, t in enumerate(tracks[:5])
)
),
colour=ctx.author.colour,
timestamp=dt.datetime.utcnow()
)
embed.set_author(name="Query Results")
embed.set_footer(text=f"{ctx.author.display_name} kaettan", icon_url=ctx.author.avatar_url)
msg = await ctx.send(embed=embed)
for emoji in list(OPTIONS.keys())[:min(len(tracks), len(OPTIONS))]:
await msg.add_reaction(emoji)
try:
reaction, _ = await self.bot.wait_for("reaction_add", timeout = 60.0, check =_check)
except asyncio.TimeoutError:
await msg.delete()
await ctx.message.delete()
else:
await msg.delete()
return tracks[OPTIONS[reaction.emoji]]
OPTIONS = {
"1️⃣": 0,
"2⃣": 1,
"3⃣": 2,
"4⃣": 3,
"5⃣": 4,
}
i dont recomand to use lavalink but i have a great music cog file for you to use just get rid of the blacklist user part and if you want it add me on discord RageWire#0001
-From head dev and support team from rage bots
import discord
from discord.ext import commands
from random import choice
import string
from discord.ext.commands.cooldowns import BucketType
import asyncio
import youtube_dl
import pafy
import datetime
from discord_slash import cog_ext, SlashContext
x = datetime.datetime.now()
from ult import *
class music(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.song_queue = {}
self.setup()
def setup(self):
for guild in self.bot.guilds:
self.song_queue[guild.id] = []
async def check_queue(self, ctx):
if len(self.song_queue[ctx.guild.id]) > 0:
ctx.voice_client.stop()
await self.play_song(ctx, self.song_queue[ctx.guild.id][0])
self.song_queue[ctx.guild.id].pop(0)
async def search_song(self, amount, song, get_url=False):
info = await self.bot.loop.run_in_executor(None, lambda: youtube_dl.YoutubeDL({"format" : "bestaudio", "quiet" : True}).extract_info(f"ytsearch{amount}:{song}", download=False, ie_key="YoutubeSearch"))
if len(info["entries"]) == 0: return None
return [entry["webpage_url"] for entry in info["entries"]] if get_url else info
async def play_song(self, ctx, song):
url = pafy.new(song).getbestaudio().url
ctx.voice_client.play(discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(url)), after=lambda error: self.bot.loop.create_task(self.check_queue(ctx)))
ctx.voice_client.source.volume = 0.5
#commands.command()
async def pause(self, ctx):
if await get_mute(ctx.author) != 0:
await ctx.send("You are blacklisted from the bot")
else:
ctx.voice_client.pause()
await ctx.send("*Paused -* ⏸️")
#commands.command()
async def resume(self, ctx):
if await get_mute(ctx.author) != 0:
await ctx.send("You are blacklisted from the bot")
else:
ctx.voice_client.resume()
await ctx.send("*Resuming -* ▶️")
#commands.command()
#commands.is_owner()
#commands.cooldown(1, 5, commands.BucketType.user)
async def oskip(self, ctx):
commandd = "oskip"
print(f"{ctx.author.name}, {ctx.author.id} used command "+commandd+" used at ")
print(x)
print(" ")
await ctx.send("skipping")
ctx.voice_client.stop()
await self.check_queue(ctx)
#commands.command()
#commands.cooldown(1, 5, commands.BucketType.user)
async def join(self, ctx):
if await get_mute(ctx.author) != 0:
await ctx.send("You are blacklisted from the bot")
else:
commandd = "join"
print(f"{ctx.author.name}, {ctx.author.id} used command "+commandd+" used at ")
print(x)
print(" ")
if ctx.author.voice is None:
return await ctx.send("You are not connected to a voice channel, please connect to the channel you want the bot to join.")
if ctx.voice_client is not None:
await ctx.voice_client.disconnect()
await ctx.author.voice.channel.connect()
#commands.command()
#commands.cooldown(1, 5, commands.BucketType.user)
async def leave(self, ctx):
if await get_mute(ctx.author) != 0:
await ctx.send("You are blacklisted from the bot")
else:
commandd = "leave"
print(f"{ctx.author.name}, {ctx.author.id} used command "+commandd+" used at ")
print(x)
print(" ")
if ctx.voice_client is not None:
return await ctx.voice_client.disconnect()
await ctx.send("I am not connected to a voice channel.")
#commands.command()
async def play(self, ctx, *, song=None):
if await get_mute(ctx.author) != 0:
await ctx.send("You are blacklisted from the bot")
else:
commandd = "play"
print(f"{ctx.author.name}, {ctx.author.id} used command "+commandd+" used at ")
print(x)
print(" ")
if song is None:
return await ctx.send("You must include a song to play.")
if ctx.voice_client is None:
return await ctx.send("I must be in a voice channel to play a song.")
# handle song where song isn't url
if not ("youtube.com/watch?" in song or "https://youtu.be/" in song):
await ctx.send("Searching for song, this may take a few seconds.")
result = await self.search_song(1, song, get_url=True)
if result is None:
return await ctx.send("Sorry, I could not find the given song, try using my search command.")
song = result[0]
if ctx.voice_client.source is not None:
queue_len = len(self.song_queue[ctx.guild.id])
if queue_len < 10:
self.song_queue[ctx.guild.id].append(song)
return await ctx.send(f"I am currently playing a song, this song has been added to the queue at position: {queue_len+1}.")
else:
return await ctx.send("Sorry, I can only queue up to 10 songs, please wait for the current song to finish.")
await self.play_song(ctx, song)
await ctx.send(f"Now playing: {song}")
#commands.command()
#commands.cooldown(1, 5, commands.BucketType.user)
async def search(self, ctx, *, song=None):
if await get_mute(ctx.author) != 0:
await ctx.send("You are blacklisted from the bot")
else:
commandd = "search"
print(f"{ctx.author.name}, {ctx.author.id} used command "+commandd+" used at ")
print(x)
print(" ")
if song is None: return await ctx.send("You forgot to include a song to search for.")
await ctx.send("Searching for song, this may take a few seconds.")
info = await self.search_song(5, song)
embed = discord.Embed(title=f"Results for '{song}':", description="*You can use these URL's to play an exact song if the one you want isn't the first result.*\n", colour=discord.Color.green())
amount = 0
for entry in info["entries"]:
embed.description += f"[{entry['title']}]({entry['webpage_url']})\n"
amount += 1
embed.set_footer(text=f"Displaying the first {amount} results.")
await ctx.send(embed=embed)
#commands.command()
#commands.cooldown(1, 5, commands.BucketType.user)
async def queue(self, ctx):
if await get_mute(ctx.author) != 0:
await ctx.send("You are blacklisted from the bot")
else: # display the current guilds queue
commandd = "queue"
print(f"{ctx.author.name}, {ctx.author.id} used command "+commandd+" used at ")
print(x)
print(" ")
if len(self.song_queue[ctx.guild.id]) == 0:
return await ctx.send("There are currently no songs in the queue.")
embed = discord.Embed(title="Song Queue", description="", colour=discord.Color.green().dark_gold())
i = 1
for url in self.song_queue[ctx.guild.id]:
embed.description += f"{i}) {url}\n"
i += 1
embed.set_footer(text="Thanks for using me!")
await ctx.send(embed=embed)
#commands.command()
#commands.has_permissions(administrator=True)
#commands.cooldown(1, 5, commands.BucketType.user)
async def adskip(self, ctx):
if await get_mute(ctx.author) != 0:
await ctx.send("You are blacklisted from the bot")
else:
commandd = "adskip"
print(f"{ctx.author.name}, {ctx.author.id} used command "+commandd+" used at ")
print(x)
print(" ")
await ctx.send("skipping")
ctx.voice_client.stop()
await self.check_queue(ctx)
#commands.command()
#commands.cooldown(1, 5, commands.BucketType.user)
async def skip(self, ctx):
if await get_mute(ctx.author) != 0:
await ctx.send("You are blacklisted from the bot")
else:
commandd = "skip"
print(f"{ctx.author.name}, {ctx.author.id} used command "+commandd+" used at ")
print(x)
print(" ")
if ctx.voice_client is None:
return await ctx.send("I am not playing any song.")
if ctx.author.voice is None:
return await ctx.send("You are not connected to any voice channel.")
if ctx.author.voice.channel.id != ctx.voice_client.channel.id:
return await ctx.send("I am not currently playing any songs for you.")
poll = discord.Embed(title=f"Vote to Skip Song by - {ctx.author.name}#{ctx.author.discriminator}", description="**80% of the voice channel must vote to skip for it to pass.**", colour=discord.Color.blue())
poll.add_field(name="Skip", value=":white_check_mark:")
poll.add_field(name="Stay", value=":no_entry_sign:")
poll.set_footer(text="Voting ends in 15 seconds.")
poll_msg = await ctx.send(embed=poll) # only returns temporary message, we need to get the cached message to get the reactions
poll_id = poll_msg.id
await poll_msg.add_reaction(u"\u2705") # yes
await poll_msg.add_reaction(u"\U0001F6AB") # no
await asyncio.sleep(15) # 15 seconds to vote
poll_msg = await ctx.channel.fetch_message(poll_id)
votes = {u"\u2705": 0, u"\U0001F6AB": 0}
reacted = []
for reaction in poll_msg.reactions:
if reaction.emoji in [u"\u2705", u"\U0001F6AB"]:
async for user in reaction.users():
if user.voice.channel.id == ctx.voice_client.channel.id and user.id not in reacted and not user.bot:
votes[reaction.emoji] += 1
reacted.append(user.id)
skip = False
if votes[u"\u2705"] > 0:
if votes[u"\U0001F6AB"] == 0 or votes[u"\u2705"] / (votes[u"\u2705"] + votes[u"\U0001F6AB"]) > 0.79: # 80% or higher
skip = True
embed = discord.Embed(title="Skip Successful", description="***Voting to skip the current song was succesful, skipping now.***", colour=discord.Color.green())
if not skip:
embed = discord.Embed(title="Skip Failed", description="*Voting to skip the current song has failed.*\n\n**Voting failed, the vote requires at least 80% of the members to skip.**", colour=discord.Color.red())
embed.set_footer(text="Voting has ended.")
await poll_msg.clear_reactions()
await poll_msg.edit(embed=embed)
if skip:
ctx.voice_client.stop()
await self.check_queue(ctx)
def setup(bot):
bot.add_cog(music(bot))
I starting to learning discord.py and I got some idea to make a anti advertising link function. My plan is detect a link that have http or https in link they send and give they a role and in my plan this role name is gg and this is my code
#Advertising link detection
#client.event
async def on_message(message):
if 'https' in message.content.lower():
await message.delete()
await message.channel.send(f"{message.author.mention} This link not ALLOWED")
else:
await client.process_commands(message)
#client.event
async def on_message(message):
if 'http' in message.content.lower():
await message.delete()
await message.channel.send(f"{message.author.mention} This link not ALLOWED")
else:
await client.process_commands(message)
#autorole
autorole = discord.utils.get(ctx.guild.roles, name='gg')
await ctx.add.roles(autorole)
I really no idea how to do this function
So in your code you have two functions with the same name, so the second one overrides the first one and only the second one is used and the first one is discarded. Most of the time situations like this can be avoided by using a single event with the code combined. in your case that would work too using a elif or the or keyword. so
#client.event
async def on_message(message):
if 'https' in message.content.lower() or 'http' in message.content.lower():
await message.delete()
await message.channel.send(f"{message.author.mention} This link not ALLOWED")
else:
await client.process_commands(message)
but I would highly suggest using regex to match for urls
You have called same event 2 times and that causes bot to stuck use else if two add 2 if statement
You have 2 events
on_message(message):
I try to create automod system. I find this code, and I try to fix it. It doesn't work. I don't know how to fix it. Do you want to give me some help to fix it? I have been working on this for 2 hours, and I cannot figure out the solution. I maybe need to import anything. I don't know...
filtered_words = ["bad", "words", "here"]
filtered_words_counter = {}
#client.event
async def on_message(message):
for word in filtered_words:
if word in message.content.lower()
await message.delete()
embed = discord.Embed(color=0xff0000)
embed.set_author(name="Banned Word")
embed.add_field(name="Username:", value=f"{message.author.mention}", inline=False)
embed.add_field(name="Banned Word:", value=f"{word}", inline=False)
embed.add_field(name="Message:", value=f"{message.content}", inline=False)
embed.set_footer(text=f'User ID: {message.author.id}')
embed.timestamp = datetime.datetime.utcnow()
channel = client.get_channel(CHANNEL_ID_HERE)
await channel.send(embed=embed)
if not filtered_words_counter.get(message.author.id):
filtered_words_counter[message.author.id] = 1
else:
filtered_words_counter[message.author.id] += 1
if filtered_words_counter[message.author.id] >= 3:
role = discord.utils.get(message.guild.roles, name="Muted")
await message.channel.send('User was muted')
await message.author.add_roles(role)
filtered_words_counter[message.author.id] = 0
I have looked at your code and have made a simpler solution.
import discord
# banned word dictionary
banned_words=[
"bad_word_1"
]
# On message event
#client.event
async def on_message(message):
if message.content in banned_words:
#you can customize the embed
embed = discord.Embed(title="Banned Word", description=f"Username: {message.author}" + "\n" + f"Banned Word: {message.content}" + "\n" + f"Message ID: {message.id}", color=0xFF0000)
embed.set_footer(text=f"User ID: {message.author.id}")
# Send the embed
await message.channel.send(embed=embed)
My solution above is the way I would do it.
However below I have made some edits to your code above.
import discord
import datetime
filtered_words = ["bad", "words", "here"]
filtered_words_counter = {}
#client.event
async def on_message(message):
for word in filtered_words:
if word in message.content.lower():
await message.delete()
embed = discord.Embed(color=0xff0000)
embed.set_author(name="Banned Word")
embed.add_field(name="Username:", value=f"{message.author.mention}", inline=False)
embed.add_field(name="Banned Word:", value=f"{word}", inline=False)
embed.add_field(name="Message:", value=f"{message.content}", inline=False)
embed.set_footer(text=f'User ID: {message.author.id}')
embed.timestamp = datetime.datetime.utcnow()
channel = client.get_channel(CHANNEL_ID_HERE)
await channel.send(embed=embed)
if not filtered_words_counter.get(message.author.id):
filtered_words_counter[message.author.id] = 1
else:
filtered_words_counter[message.author.id] += 1
if filtered_words_counter[message.author.id] >= 3:
role = discord.utils.get(message.guild.roles, name="Muted")
await message.channel.send('User was muted')
await message.author.add_roles(role)
filtered_words_counter[message.author.id] = 0 ```
I was trying to make my code a little cleaner by using cogs but it doesn't seem to work. When I do +balance this error comes up: Ignoring exception in command None: discord.ext.commands.errors.CommandNotFound: Command "balance" is not found: Here's the code part in main.py:
for file in os.listdir("./cogs"): # lists all the cog files inside the cog folder.
if file.endswith(".py"): # It gets all the cogs that ends with a ".py".
name = file[:-3] # It gets the name of the file removing the ".py"
bot.load_extension(f"cogs.{name}") # This loads the cog.
And from the balance file:
bot = commands.Bot(command_prefix='+')
class Balance(commands.Cog):
def __init__(self, client):
self.bot = bot
#commands.command(aliases = ["bal"])
async def balance(self, ctx):
await open_account(ctx.author)
user = ctx.author
users = await get_bank_data()
wallet_amt = users[str(user.id)]["wallet"]
#bank_amt = users[str(user.id)]["bank"]
em = discord.Embed(title = f"{ctx.author.name}'s balance",color = (0x95a5a6), description = f"{wallet_amt} dogecoins")
#em.add_field(name = "Bank balance",value = bank_amt)
await ctx.send(embed = em)
async def open_account(user):
users = await get_bank_data()
if str(user.id) in users:
return False
else:
users[str(user.id)] = {}
users[str(user.id)]["wallet"] = 250
users[str(user.id)]["bank"] = 0
with open("mainbank.json","w") as f:
json.dump(users,f)
return True
async def get_bank_data():
with open("mainbank.json","r") as f:
users = json.load(f)
return users
async def update_bank(user,change = 0,mode = "wallet"):
users = await get_bank_data()
users[str(user.id)][mode] += change
with open("mainbank.json","w") as f:
json.dump(users,f)
return True
def setup(bot):
bot.add_cog(Balance(bot))
I'm new to coding and cogs especially so if anyone knows how to fix this, please help me.
I can't tell if the commands and functions you're showing are inside the class or not. Also, why are you creating a new Bot instance, and why does your init function take an argument called client and then set self.bot to bot?
Try replacing client in your init function with bot, or vice versa.
Do you get any errors when you try to run your main file?