Cannot get role to change colours - python

I've got a piece of code that takes a specified role and that roles server and puts it into a list, and any roles that are in the list should change colour but well.... it doesn't.
code:
#client.command(pass_context=True,description="Linusifies a role")
#has_permissions(administrator = True)
async def rgb(ctx, role : discord.Role):
server = ctx.message.author.server
if (("{}, {}").format(server, role)) in rgb_list:
msg = "{}'s rgb is turned off :disappointed_relieved:".format(role)
rgb_list.remove(("{}, {}").format(server, role))
await client.send_message(ctx.message.channel, msg)
print (server)
else:
msg = "{}'s rgb has been turned on :christmas_tree:".format(role)
rgb_list.append(("{}, {}").format(server, role))
await client.send_message(ctx.message.channel, msg)
print (server)
and in my #client.event:
async def rgb_():
await client.wait_until_ready()
while not client.is_closed:
print (rgb_list)
await client.edit_role(rgb_list,colour=discord.Colour(0x1abc9c))
await asyncio.sleep(2)
await client.edit_role(rgb_list,colour=discord.Colour(0x11806a))
await asyncio.sleep(2)
await client.edit_role(rgb_list,colour=discord.Colour(0x2ecc71))
await asyncio.sleep(2)
await client.edit_role(rgb_list,colour=discord.Colour(0x1f8b4c))
await asyncio.sleep(2)
await client.edit_role(rgb_list,colour=discord.Colour(0x3498db))
await asyncio.sleep(2)
await client.edit_role(rgb_list,colour=discord.Colour(0x206694))
await asyncio.sleep(2)
await client.edit_role(rgb_list,colour=discord.Colour(0x9b59b6))
await asyncio.sleep(2)
await client.edit_role(rgb_list,colour=discord.Colour(0x71368a))
await asyncio.sleep(2)
await client.edit_role(rgb_list,colour=discord.Colour(0xe91e63))
await asyncio.sleep(2)
await client.edit_role(rgb_list,colour=discord.Colour(0xad1457))
await asyncio.sleep(2)
await client.edit_role(rgb_list,colour=discord.Colour(0xf1c40f))
await asyncio.sleep(2)
await client.edit_role(rgb_list,colour=discord.Colour(0xc27c0e))
await asyncio.sleep(2)
await client.edit_role(rgb_list,colour=discord.Colour(0xe67e22))
await asyncio.sleep(2)
await client.edit_role(rgb_list,colour=discord.Colour(0xa84300))
await asyncio.sleep(2)
await client.edit_role(rgb_list,colour=discord.Colour(0xe74c3c))
await asyncio.sleep(2)
await client.edit_role(rgb_list,colour=discord.Colour(0x992d22))
await asyncio.sleep(2)

Read the documentation for Client.edit_role. edit_role takes a Server and a Role, not a list of strings. You should be storing your list of roles as a list of Roles. Then you can loop over that lsit to change the colours.
#client.command(pass_context=True,description="Linusifies a role")
#has_permissions(administrator = True)
async def rgb(ctx, role : discord.Role):
server = ctx.message.author.server
if role in rgb_list:
msg = "{}'s rgb is turned off :disappointed_relieved:".format(role.name)
rgb_list.remove(role)
await client.send_message(ctx.message.channel, msg)
print (server)
else:
msg = "{}'s rgb has been turned on :christmas_tree:".format(role.name)
rgb_list.append(role)
await client.send_message(ctx.message.channel, msg)
print (server)
colours = [discord.Colour(0x1abc9c), discord.Colour(0x11806a)]
#client.event
async def on_ready():
while not client.is_closed:
for colour in colours: # make a list of Colours
for role in rgb_list:
await client.edit_role(role.server, role, colour=colour)
await asyncio.sleep(2)

Related

Why is my bot.command function not working?

So basically I have this problem, any command that I type does not work. There are no errors, and everything else is working fine. It's just that for some reasons #bot.command() isn't working, and that is kind of annoying.
import discord
from discord.utils import get
from discord.ext import commands
import time
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
bot = commands.Bot(command_prefix = '$', intents=intents)
TOKEN = 'hi'
ROLE = 'hi'
db1 = [hi, hi]
#bot.command()
async def test(ctx, arg):
await ctx.send(arg)
await bot.process_commands(arg)
#bot.event
async def on_member_join(member):
role = get(member.guild.roles, name=ROLE)
await member.add_roles(role)
await member.send('hi')
try:
channel = member.guild.system_channel
embedVar = discord.Embed(title="Welcome <#{}> in {} ".format(str(member.id),str(member.guild)), description="hi", color=0x00ff00)
await channel.send(embed=embedVar)
except:
channel = member.guild.get_channel(hi)
embedVar = discord.Embed(title="Welcome <#{}> in {} ".format(str(member.id),str(member.guild)), description="hi", color=0x00ff00)
await channel.send(embed=embedVar)
#bot.event
async def on_member_remove(member):
try:
channel = member.guild.system_channel
embedVar = discord.Embed(title="Bye {} from {} ".format(str(member.name),str(member.guild)), description="Come back when you want", color=0x00ff00)
await channel.send(embed=embedVar)
except:
channel = member.guild.get_channel(hi)
embedVar = discord.Embed(title="Bye {} from {} ".format(str(member.name),str(member.guild)), description="Come back when you want", color=0x00ff00)
await channel.send(embed=embedVar)
#bot.event
async def on_invite_create(invite):
channel = bot.get_channel(hi)
await channel.send("An invite has been created, {}, by <#{}> on {}".format(str(invite.url), str(invite.inviter.id), str(invite.created_at)))
#bot.event
async def on_invite_delete(invite):
channel = bot.get_channel(hi)
await channel.send("An invite has been deleted by{}".format(str(invite.inviter.id)))
#bot.event
async def on_member_ban(guild, member):
channel = bot.get_channel(hi)
embedVar = discord.Embed(title="Ban", description="Ban requested on<#{}>".format(str(member.id)))
await channel.send(embed=embedVar)
#bot.event
async def on_member_unban(guild, member):
channel = bot.get_channel(hi)
embedVar = discord.Embed(title="Unban", description="Unban requested on<#{}>".format(str(member.id)))
await channel.send(embed=embedVar)
#bot.event
async def on_ready():
print(f'{bot.user} succesfully logged in')
return
#bot.event
async def on_message(message):
if message.content.startswith('purge requested by'):
time.sleep(1)
await message.delete()
if message.author == bot:
return
if message.content == 'hi':
await message.channel.send('hi')
if message.content.startswith('binvites'):
totalInvites = 0
for i in await message.guild.invites():
if i.inviter == message.author:
totalInvites += i.uses
await message.channel.send(f"You have invited {totalInvites} member{'' if totalInvites == 1 else 's'} to the Central Trade server")
if message.content == 'bpurge':
if message.author.id in db1:
await message.channel.purge(limit=10)
await message.channel.send('purge requested by <#{}>'.format(str(message.author.id)))
else:
return
if message.content == 'block':
if message.author.id in db1:
channel = message.channel
overwrite = channel.overwrites_for(message.guild.default_role)
overwrite.send_messages = False
await channel.set_permissions(message.guild.default_role, overwrite=overwrite)
embedVar = discord.Embed(title="Lock", description="Channel lock request by <#{}>".format(str(message.author.id)), color= 0x00FFFF)
await message.channel.send(embed=embedVar)
else:
await message.author.send('You do not have the permission to use this command')
if message.content == 'bunlock':
if message.author.id in db1:
channel = message.channel
overwrite = channel.overwrites_for(message.guild.default_role)
overwrite.send_messages = True
await channel.set_permissions(message.guild.default_role, overwrite=overwrite)
embedVar = discord.Embed(title="Unlock", description="Channel unlock request by <#{}>".format(str(message.author.id)), color=0xC0C0C0)
await message.channel.send(embed=embedVar)
else:
await message.author.send('You do not have the permission to use this command')
if message.content == 'test':
embedVar = discord.Embed(title="test", description="test", color=0x00ff00)
embedVar.add_field(name="test", value="test", inline=False)
embedVar.add_field(name="test", value="test", inline=False)
await message.channel.send(embed=embedVar)
if message.content == 'bpurges':
if message.author.id in db1:
await message.channel.purge(limit=10000)
await message.channel.send('purge requested by <#{}>'.format(str(message.author.id)))
embedVar = discord.Embed(title="Purge", description="Purge requested by<#{}>".format(str(message.author.id)))
await message.channel.send(embed=embedVar)
time.sleep(1)
await message.channel.delete()
bot.run(TOKEN)
Anyone has an idea why it's not working ? Also seems like i need to post more details, don't pay attention to this : Roméo et Juliette (Romeo and Juliet) est une tragédie de William Shakespeare.

Getting discord.py to talk using tts

How would I get a my bot to use /tts in a vc?
Here is the code that I have so far. It is a loop of one word and I want to use tts in the voice channel I am in.
#client.command()
async def start(ctx):
global start_channel
start_channel = ctx.channel.id
reminder.start()
await ctx.send('Bot has Started')
#tasks.loop(minutes=10)
async def reminder():
channel = client.get_channel(int(start_channel))
await channel.send('/tts Honk!')
#client.command()
async def stop(ctx):
reminder.cancel()
await ctx.send('Bot has stopped')
#client.command()
async def join(ctx):
if ctx.author.voice is None:
await ctx.send("You're not in a voice channel")
voice_channel = ctx.author.voice.channel
if ctx.voice_client is None:
await voice_channel.connect()
else:
await ctx.voice_channel.move_to(voice_channel)
add tts as = True to allow it use tts commands through discord
#tasks.loop(minutes=10)
async def reminder():
channel = client.get_channel(int(start_channel))
await channel.send('Honk!', tts=True)

auto add reaction to message attachment in channel

so i made a code that if a person sends an attachment ina certain channel it would react to it but I can't seem to make it work
Here's my code
#bot.event
async def on_message(message):
if message.attachments in message.channel.name == "memes-only":
await asyncio.sleep(2)
await message.add_reaction("😭")
await message.add_reaction("🥲")
channel = message.channel
await channel.send("i have reacted")
await message.delete()
i have also tried it with if message.attachments in message.channel.id == "860073616606625792":
edit: my traceback says TypeError: argument type of int is not iterable
Okay, so you have this weird if statement
if message.attachments in message.channel.name == "memes-only":
the thing it is doing is first checking is message.attackments is in message.channel.name == 'memes-only' which is a boolean value
what you want to do is
#bot.event
async def on_message(message):
if message.channel.name == "memes-only":
await asyncio.sleep(2)
await message.add_reaction("😭")
await message.add_reaction("🥲")
channel = message.channel
await channel.send("i have reacted")
await message.delete()

getting error code message is not defined

Iv been working on this bot which when you type "/court #user time" it will give them the role jail
and after the given amount of time it will remove it but when i run it it doesn't work and it says:
"discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'message' is not defined"
import discord
from discord.ext import commands
import ctx
import re
import time
from time import sleep
PREFIX = "$"
bot = commands.Bot(command_prefix=PREFIX, description="Hi")
#bot.event
async def on_ready():
print('Logged on as', bot.user)
channel = bot.get_channel(717005397655027805)
#await channel.send("I am now online")
activity = discord.Game(name="eb!help", type=3)
await bot.change_presence(status=discord.Status.online, activity=activity)
#bot.command(name='court')
#commands.has_role('Server Developer')
async def court(ctx, user_mentioned, time):
messageContent = message.content
if len(messageContent) > 0:
if re.search("^send.*court$", messageContent):
user_id = message.mentions[0].id
user = message.mentions[0]
await message.channel.send(
f"sending <#{user_id}> to court!"
)
role = discord.utils.get(message.guild.roles, name="Jail")
await user.add_roles(role)
sleep(time)
await bot.remove_roles(user, role)
bot.run('TOKEN_GOES_HERE')
The error message is pretty clear,
messageContent = message.content.
message isn't defined in the scope here.
We can get the message with ctx.message
#bot.command(name='court')
#commands.has_role('Server Developer')
async def court(ctx, user_mentioned, time):
message = ctx.message
messageContent = message.content
Doing this will work fine, but you're entirely misusing ext.commands here.
You could do
#bot.command(name='court')
#commands.has_role('Server Developer')
async def court(ctx, user_mentioned: discord.Member, time: int):
role = discord.utils.get(message.guild.roles, name="Jail")
await user_mentioned.add_roles(role)
await asyncio.sleep(time)
await user_mentioned.remove_roles(role)
time.sleep is blocking and bot doesn't have the attribute remove_roles, please refer to the docs for information.
#ceres here's the court command code:
bot.command(name='court')
#commands.has_role('Server Developer')
async def court(ctx, user_mentioned: discord.Member, time: int):
print("test")
user_id = message.mentions[0].id
user = message.mentions[0]
role = discord.utils.get(message.guild.roles, name="Jail")
await user_mentioned.add_roles(role)
await ctx.send(
f"sending <#{user_id}> to court!"
)
await asyncio.sleep(time)
await user_mentioned.remove_roles(role)
You can get message's content by typing ctx.message.content but you don't need it. If you describe the parameter as discord.Member, it will return a member object if it founds else bot will raise commands.BadArgument. Also you should use asyncio.sleep for async sleep. This will not block your bot's other commands.
import asyncio
#bot.command(name='court')
#commands.has_role('Server Developer')
async def court(ctx, user: discord.Member, time: int):
role = discord.utils.get(message.guild.roles, name="Jail")
if role is not None:
await ctx.send(
f"sending {user.mention} to court!"
)
await user.add_roles(role)
await asyncio.sleep(time)
await user.remove_roles(role)
else:
await ctx.send("Role `Jail` is not exist.")
bot.run('TOKEN')

How can I detect reactions?

I'm attempting to make an offer command in which the bot detects whether the mentioned user reacts with check or x.
#bot.command()
#commands.has_any_role("Franchise Owner", "General Manager", "Head Coach")
async def offer(ctx, member:discord.Member, reaction):
embed = discord.Embed()
embed.add_field(name="<a:Loading:768095883664424971> Incoming Offer", value=f"The <:DallasCowboys:788796627161710592> have offered {member.mention}.")
offer_sent = await ctx.send(embed=embed)
await offer_sent.add_reaction("<a:CheckMark:768095274949935146>")
await offer_sent.add_reaction("<a:XMark:768095331555606528>")
await member.send("You have been offered to the <:DallasCowboys:788796627161710592>. You have 30 minutes to accept/decline.")
await asyncio.sleep(1800) # Replace "30" with 1800, because 1800 in seconds is 30 min.
await offer_sent.delete()
def on_reaction(reaction, member:discord.Member):
channel = member.channel
def check(reaction, user):
return user == member and str(reaction.emoji) == '<a:CheckMark:768095274949935146>'
try:
reaction, user = await bot.wait_for('reaction_add', timeout=60.0, check=check)
except asyncio.TimeoutError:
await channel.send(f"{member.mention} hasn't reacted in time.")
else:
await channel.send(f"{member.mention} has accepted <:DallasCowboys:788796627161710592> offer.")
I removed the reaction from the offer function as I have no clue what it did as it wasn't used in the code. Then I removed the on_reaction function but moved the code from inside it before the asyncio.sleep(1800).
#bot.command()
#commands.has_any_role("Franchise Owner", "General Manager", "Head Coach")
async def offer(ctx, member:discord.Member):
embed = discord.Embed()
embed.add_field(name="<a:Loading:768095883664424971> Incoming Offer", value=f"The <:DallasCowboys:788796627161710592> have offered {member.mention}.")
offer_sent = await ctx.send(embed=embed)
await offer_sent.add_reaction("<a:CheckMark:768095274949935146>")
await offer_sent.add_reaction("<a:XMark:768095331555606528>")
await member.send("You have been offered to the <:DallasCowboys:788796627161710592>. You have 30 minutes to accept/decline.")
channel = ctx.channel
def check(reaction, user):
return user == member and str(reaction.emoji) == '<a:CheckMark:768095274949935146>'
try:
reaction, user = await bot.wait_for('reaction_add', timeout=60.0, check=check)
except asyncio.TimeoutError:
await channel.send(f"{member.mention} hasn't reacted in time.")
else:
await channel.send(f"{member.mention} has accepted <:DallasCowboys:788796627161710592> offer.")
await asyncio.sleep(1800) # Replace "30" with 1800, because 1800 in seconds is 30 min.
await offer_sent.delete()

Categories

Resources