Discord.py rewrite bot won't change my nickname - python

I've been learning Python 3.8 by building a Discord bot for my small server of friends. I want my friends to be able to change my nickname, but Discord doesn't seem to allow that unless I give everyone the same rank.
My idea was to give my bot the highest role and let them use a bot command to change my nickname (which would be the second-highest role), but I get the following error:
Traceback (most recent call last):
File "/home/cqqlguy/.local/lib/python3.8/site-packages/discord/client.py", line 333, in _run_event
await coro(*args, **kwargs)
File "/home/cqqlguy/CQQLBOT/CQQLBOT.py", line 516, in on_message
await cqqluser.edit(nick = newnick)
File "/home/cqqlguy/.local/lib/python3.8/site-packages/discord/member.py", line 601, in edit
await http.edit_member(guild_id, self.id, reason=reason, **payload)
File "/home/cqqlguy/.local/lib/python3.8/site-packages/discord/http.py", line 241, in request
raise Forbidden(r, data)
discord.errors.Forbidden: 403 Forbidden (error code: 50013): Missing Permissions
The relevant code looks like this:
#bot.event
async def on_message(message):
lowermess = message.content
dMess = lowermess.lower()
if dMess.startswith("$changenick "):
newnick = lowermess.split(" ", 1)[1] #whatever the user enters after the command
changeuser = guild.get_member(id) #id would be my user ID
await changeuser.edit(nick = newnick)
This seems to work on users that aren't me, but the bot has been given a higher role than everyone so I'm not sure why it's not changing it for me.

Related

My discord bot is giving me permission errors

The problem is that my bot has administrator permissions and I don't see how or why I am getting permission errors. This error pops up when the bot should be giving a role to a joining member.
#bot.event
async def on_member_join(member):
welcome_channel = discord.utils.get(member.guild.text_channels, name='welcome_channel')
guild=member.guild
if guild.member_count % 10 ==1: #to find the last digit of the member count to see if st or nd or rd or th should be placed at the end
embed=discord.Embed()
embed2=discord.Embed()
name=(member)
embed.set_author(name=f"{random.choice(greetings)} {name}. You are the {guild.member_count}st member", icon_url=member.avatar_url)
await welcome_channel.send(embed=embed)
embed2.set_thumbnail(url=member.avatar_url)
await welcome_channel.send(f"{random.choice(greetings)} {member.mention}. You are the {guild.member_count}st member", embed=embed2)
The exact errors that I get are:
Traceback (most recent call last):
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "c:\Users\Lenovo\Desktop\Python\VSC\Discord Bot\Main backup.py", line 53, in on_message
await message.author.add_roles(role)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\member.py", line 777, in add_roles
await req(guild_id, user_id, role.id, reason=reason)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\http.py", line 248, in request
raise Forbidden(r, data)
discord.errors.Forbidden: 403 Forbidden (error code: 50013): Missing Permissions
As said in Traceback, the error probably doesn't come from on_member_join(), but from the on_message() event
You should check the order of the roles of your server. If bot's role is below the role you want to give to a user, then your bot doesn't have the permission to do so (as explained here)
Example :

I want to make my bot "listening" to something

I am trying to make my discord bots status say listening to you. I have tried various metods I found. This is the only one that haven't made my bot stop working:
#client.event
async def on_ready():
client.user.setActivity('you', { type: 'LISTENING' });
With this my bot is still up, but it doesnt show a status. I also get this error message:
Ignoring exception in on_ready
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "main.py", line 9, in on_ready
client.user.setActivity('to you', { type: 'LISTENING' });
AttributeError: 'ClientUser' object has no attribute 'setActivity'
Please ask I forgot to include something important.
Read the links:
https://discordpy.readthedocs.io/en/stable/api.html#discord.Client.change_presence
https://discordpy.readthedocs.io/en/stable/api.html#discord.Client.activity
https://discordpy.readthedocs.io/en/stable/api.html#discord.ActivityType
https://discordpy.readthedocs.io/en/stable/api.html#discord.Spotify.type
setActivity does not exist. ActivityType.listening only exists via discord.Spotify.
You change your activity with change_presence:
game = discord.Game("with the API")
await client.change_presence(status=discord.Status.idle, activity=game)
song = discord.Spotify(...)
await client.change_presence(status=discord.Status.idle, activity=song)
setActivity is from discord.js. You are however programming in Python and you are using discord.py.

(discord.py) İ want to make a roll call bot for a discord server

I want to make a discord bot. Here is what i want: When i say '!yoklama 11.10.2020.txt'(or something) bot sends me a list of participants of which voice channel i am in.[not 1 channel(which i am in)] But i dont know how can i do.
Then i was some research and i find similar things about my request.
But they did not work properly.
Here is my found codes:
import discord
from discord.ext.commands import Bot
from discord.ext import commands
client = commands.Bot(command_prefix='!')
#client.command()
async def attendance(ctx, filename):
channel = client.get_channel(755129236397752441) # Replace with voice channel ID
with open(filename, 'w') as file:
for member in channel.members:
file.write(member.name + '\n')
client.run('mytoken')
This codes are working(when i run they dont give me error) but when i say !attendence test.txt bot
does not say anything and my python shell say to me:
Ignoring exception in command attendance:
Traceback (most recent call last):
File "C:\Users\Yunus Emre KISA\AppData\Local\Programs\Python\Python38-32\lib\site-
packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(args, **kwargs)
File "C:\bot2\bot2.py", line 10, in attendance
for member in channel.members:
AttributeError: 'NoneType' object has no attribute 'members'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Yunus Emre KISA\AppData\Local\Programs\Python\Python38-32\lib\site-
packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\Yunus Emre KISA\AppData\Local\Programs\Python\Python38-32\lib\site-
packages\discord\ext\commands\core.py", line 859, in invoke
await injected(ctx.args, **ctx.kwargs)
File "C:\Users\Yunus Emre KISA\AppData\Local\Programs\Python\Python38-32\lib\site-
packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError:
'NoneType' object has no attribute 'members
You see guys i dont understand anything please help me.
I found these question but it does not work for me:
Discord Python bot creating a file of active member in a vocal channel
#client.command()
async def test(ctx):
guild = ctx.guild
channel = discord.utils.get(guild.voice_channels, name='General')
for member in channel.members:
print('test')
What I would do instead of having to pass in the file in the command, is just in the function writing:
with open("file.txt", "w") as f:

Issue with playing audio - discord.py

ok, so I added this to my bot:
if message.content.startswith('!test'):
voice = await client.join_voice_channel(message.author.voice.voice_channel)
args = message.content.split(" ")
betterargs = " ".join(args[1:])
player = await voice.create_ytdl_player('https://www.youtube.com/watch?v=' + betterargs)
player.start()
but when I type !test and with the end of a youtube link in the chat, the bot joins the channel, but it gives me this error
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\server\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\client.py", line 307, in _run_event
yield from getattr(self, event)(*args, **kwargs)
File "noob.py", line 99, in on_message
voice = await client.join_voice_channel(message.author.voice.voice_channel)
File "C:\Users\server\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\client.py", line 3211, in join_voice_channel
Anyone knows whats going on?
That property (create_ytdl_player) has been removed in rewrite versions (1.x) of discord.py. Please check the example from the discord.py github repository for more info. (It has a music bot example that used youtube-dl)

Discord.py Bot that doesn't act like it has permissions

So I am working on a Discord Bot and trying to work on role assignments. I made sure that I am using the new rewrite system, I made sure I gave the bot administrative privileges. I have taken just about every measure I could think of and it simply is not working. I looked at the updated API to handle it, used sample code as a framework, This is the code
import os
import discord
from discord.utils import get as dget
from discord.ext.commands import Bot
from dotenv import load_dotenv
load_dotenv('key.env')
TOKEN = os.getenv('DISCORD_TOKEN')
client = Bot(command_prefix = '!')
#client.event
async def on_ready():
print('Connected')
#client.command()
async def role(ctx):
user = ctx.message.author
role = discord.utils.get(ctx.guild.roles, name="Sample")
await user.add_roles(role)
client.run(TOKEN)
and this is the error that i get:
Ignoring exception in command role:
Traceback (most recent call last):
File "C:\Users\chris\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
ret = await coro(*args, **kwargs)
File "d:/Google Drive/Coding/Khasbot/main.py", line 22, in role
await user.add_roles(role)
File "C:\Users\chris\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\member.py", line 641, in add_roles
await req(guild_id, user_id, role.id, reason=reason)
File "C:\Users\chris\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\http.py", line 221, in request
raise Forbidden(r, data)
discord.errors.Forbidden: 403 Forbidden (error code: 50013): Missing Permissions
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\chris\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\chris\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\chris\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 92, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: Forbidden: 403 Forbidden (error code: 50013): Missing Permissions
I honestly have no idea what could be going wrong. I have tried endless things and even created a whole new bot altogether. Still the same issue. It worked last night and literally just won't do it.
Make sure that your bot must have Manage Roles permission on your server and the role for adding roles must be lower than your bot top role.
Make you did these 2 things.

Categories

Resources