Multiple asyncs in discord.py but only first one is working - discord.py

So i startet today with python and wanted to code a discord bot. Now I ran into that problem that I have 4 asyncs all of them back to back but only the first one is working.
bot = commands.Bot(command_prefix="$")
async def Member(ctx):
await ctx.channel.send("||#Member||")
async def Supporter(ctx):
await ctx.channel.send("||#Supporter||")
async def everyone(ctx):
await ctx.channel.send("||#everyone||")```

So, I think you might be new to this. But when we post questions here we provide the code along with an error if there is one. Otherwise, how are we going to know your issue. But I am going to try to guess. Your bot.start() before the other functions because if so, the bot wont recognize them.
Needs to be like this.
#commands.has_permissions(administrator=True)
#bot.command()
async def setdefaultrole(ctx, defualtRole):
guildID = ctx.guild.id
updateDefualtRole(guildID, defualtRole)
################################ Add Defualt Role ##################################################
##commands.has_permissions(administrator=True)
##bot.command()
#async def setdefualtrole(ctx, defualtRole):
#guildID = ctx.guild.id
#updateDefualtRole(guildID, defualtRole)
bot.run("TOKEN")
See how the bot.run() or bot.start() is at the bottom of all the functions. If this is not the problem, then add a code snippet from your code and then at me. Good luck.

So the answer to this question for anyone in the future. He need to add bot.command() to every new command he was trying to make. Otherwise, it will not register as a command.
New code:
#bot.command()
async def Member(ctx):
await ctx.channel.send("||#Member||")
#bot.command()
async def Supporter(ctx):
await ctx.channel.send("||#Supporter||")
#bot.command()
async def everyone(ctx):
await ctx.channel.send("||#everyone||")

Related

Discord.py - How to run additional code after running client.run(token)?

Seemingly simple question but I'm stuck on how to solve it.
I use discord.py to login to my account and I want to DM a user by inputting his user_id .
import discord
class MyClient(discord.Client):
async def on_ready(self):
print(f'Logged in as {self.user} (ID: {self.user.id})')
client = MyClient()
client.run('token')
async def send_message(user_id):
user = client.get_user(user_id)
await user.send('My message')
When I run this python file in my python shell, it would print the "Logged in as ..." success message and it would hang up. It wouldn't let me type any other command.
I simply want to run the send_message function with a unique user_id, so I can DM a particular user.
How do I do this?
That's because client.run is blocking, so any code below it won't be executed while the bot is running. The best way to avoid this problem is by moving the function somewhere above it, like in the client subclass.
I don't know exactly where you want to execute this function from, so I would suggest that you run it from on_ready for now.
class MyClient(discord.Client):
async def on_ready(self):
print(f'Logged in as {self.user} (ID: {self.user.id})')
await self.send_message(123456) # DM a user with id "123456" as soon as the bot is ready
async def send_message(self, user_id):
user = self.get_user(user_id)
await user.send('My message')
client = MyClient()
...
If you want to execute this command from Discord, here is a good walkthrough.

How to replace ctx in app_commands? - Discord.py

I'm using the code:
#app_commands.command(name='command', description='desc command')
async def command_(self, interaction: discord.Interaction):
#body command
For example I need to know the author who sent the team. Usually (if I use not App_Commands), I in the argument of the command of the command set ctx parameter:
#commands.command(name='command', description='desc command')
async def command_(self, ctx):
await ctx.send(ctx.author.name)
But app_commands does not support ctx. So, how can you replace the ctx in app_commands?
Somehow yes, in my opinion should look code look:
#app_commands.command(name='command', description='desc command')
async def command_(self, interaction: discord.Interaction):
ctx = <replacement of ctx>
await interaction.response.send_message(ctx.author.name)
Don't know, it is probably a MessageInteracion, but I don't know how to extract information from it like from ctx.
Many things can be done from the interaction but you can use
ctx = await bot.get_context(interaction) to get the context.
Just note (happened to me) that if you want to transform your old commands to app_commands, check that the messages are sent quickly enough, or you will get an error that didn't happen with #commands.command()

is there another command should i use?

So, I can't use the "?roast" command and "?roast #(user)" at the same time...
#client.event
async def on_ready():
await client.change_presence(status=discord.Status.dnd, activity=discord.Game(' "?" Booe'))
print('Bot is ready to play!')
#client.command()
async def roast(ctx, member:discord.Member):
roast_messages = [
f'{ctx.message.author.mention}You are useless as the UEUE in Queue {member.mention}',
]
await ctx.send(random.choice(roast_messages))
There is always a way to solve that , discord gives us a way like this
#client.command()
async def roast(ctx, member:discord.Member=None):#None is if nothing is given then its None
if member == None:
member = ctx.author
roast_messages = [
f'{ctx.message.author.mention}You are useless as the UEUE in Queue {member.mention}',]
await ctx.send("imagine roasting yourself")#Your Choice Absolutely :)
await ctx.send(random.choice(roast_messages))
here if the member isnt specified , the function is called taking member as none , later gives the none value to the ctx.author itself (dont try to write ctx.author instead of none in the async def roast it gives error as ctx aint defined. also i forgot your roast messages wont make sense then so make sure to edit them too TY :)

discord.py moving people to different voice channels

channel_a = bot.get_channel(780743421442260993)
#bot.command()
async def Movee(ctx):
i = 0
while i < len(participant):
if type(participant[i]) != str:
await participant[i].move_to(channel_a)
i += 1
I want to move people to another voice chat room,
but using this code, people are just disconnected from voice chat room.
I've already checked if the channel ID is correct...
If you know the solution, I would appreciate it if you could let me know.
channel = []
#bot.event
async def on_ready():
channel_a = bot.get_channel(780743421442260993)
channel.append(channel_a)
#bot.command()
async def setChA(ctx):
channel[0] = bot.get_channel(ctx.message.author.voice.channel.id)
await ctx.send("Done")
#bot.command()
async def Move(ctx):
i = 0
while i < len(participant):
if type(participant[i]) != str:
await participant[i].move_to(channel[0])
i += 1
It work very well change as above.
I think it's caused by the way the async function works.
It will probably work well without using list.
I post it for someone who will have the same problem as me.
Also I always welcome anyone who knows a better way :D

Discord Bot sending more than one message

So I am creating a simple bot that detects when somebody joins a server and when somebody leaves the server.
I added a command to show people's avatars, but any time I do it, or when somebody joins or leaves, it sends the message more than once.
I've searched and I can't find the problem.
Can you guys help me?
Here's my code
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="?")
#client.event
async def on_ready():
print("Ready")
#client.event
async def on_member_join(member):
channel = discord.utils.get(member.guild.text_channels, name="entradas")
await channel.send(f"{member} is new on the server, everyone say hi")
show_avatar = discord.Embed(color = discord.Color.blue())
show_avatar.set_image(url="{}".format(member.avatar_url))
await channel.send(embed=show_avatar)
#client.event
async def on_member_remove(member):
channel = discord.utils.get(member.guild.text_channels, name="saidas")
await channel.send(f"{member} left the server, press F to pay respects")
#client.command()
async def avatar(ctx, member: discord.Member):
show_avatar = discord.Embed(color = discord.Color.blue())
show_avatar.set_image(url="{}".format(member.avatar_url))
await ctx.send(embed=show_avatar)
You should check if you are running 2 bots.
If you are running your bot on Linux with screen, simply check with
screen -ls
on windows, just check the task-manager and look under something like Python.
It's btw possible to have the same bot running twice.

Resources