In the given code below I was trying to give a role to the person who reacts to a message by using any emoji but this code throws me an error saying that 'Member' has no attribute 'server' what should I do now?
import discord
from discord.ext import commands
import random
client = discord.Client()
#client.event
async def on_ready():
print('ready')
#client.event
async def on_reaction_add(reaction, user):
channel = reaction.message.channel
await channel.send(f'{user.name} has reacted by using {reaction.emoji} emoji, his message was {reaction.message.content}')
role = discord.utils.get(user.server.roles, name = 'Bot')
await client.add_roles(user, role)
client.run('TOKEN')
First off take a look at the documentation. It'll tell you everything you need to know about what has what attributes. Also, server is called guild in discord.py. So use user.guild.roles instead.
Related
So i was writing a simple discord bot where when an user join it would simply send a message in the main channel saying something like hello user. Everything was working, at first i printed that a user joined on the console and everything worked. Then i tried to do it in a channel by using channel = client.get_channel("my channel here") await channel.send("Hello") but it's not working as intended. So i was wondering if there was a way to do this.
(Btw this is my first stackoverflow question, so if there's anything i did wrong pls let me know)
Here is the Full code:
from discord.ext import commands
intents = discord.Intents()
intents.members = True
client = commands.Bot(command_prefix='!', intents = intents)
#client.event
async def on_ready():
print("The bot logged on")
print("-----------------")
#client.event
async def on_member_join(member):
print("Somebody joined")
channel = client.get_channel(my channel here)
await channel.send("Hello")
client.run("MY token")
And here is the error:
AttributeError: 'NoneType' object has no attribute 'send'```
You need to await fetch, because the cache might not have the channel in there when the member joins. Your coroutine error comes from not awaiting it. (An unexecuted coroutine object gets assigned to channel, and then you get an error when it tries to call that because it was "already called".)
#client.event
async def on_member_join(member):
print("Somebody joined")
channel = await client.fetch_channel(123456789)
await channel.send("Hello")
I'm trying to get the bot's name to change every time it runs. I've check the discord.py docs but nothing there has been of use and none of them throw up any errors. Any ideas?
Has perms 'admin' and 'change nickname'
import discord
from discord.ext import commands
from discord.ext.commands import bot
bot = commands.Bot(command_prefix=PREFIX)
bot.remove_command('help')
#bot.event
async def on_ready():
await bot.user.edit(nick="New Nickname")
bot.run(TOKEN)
You must have the member object of your bot to change your nickname as nicknames are done in guild's. Then you must edit it.
#bot.event
async def on_ready():
for guild in bot.guilds:
await guild.me.edit(nick="new nickname")
As I said in the title I wanted to make my bot give me any rank I want.
I tried this and it doesn't work becouse of Unresolved attribute reference 'add_roles' for class 'Client'.
import discord
from discord.utils import get
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == '$rank':
role = get(message.server.roles, name='Człek')
await client.add_roles(message.author, role)
From the docs, Client doesn't have the method add_roles, but Member does.
Try await message.author.add_roles(role)
I was working on discord.py and I got issue with error AttributeError: 'NoneType' object has no attribute 'send'
Here is the code
import discord
from discord.ext import commands
pybot=commands.Bot(command_prefix="#", description="I love it",case_insensitive=True)
log_channel_id=674175630916583445
#pybot.event
async def on_ready():
print(f"Logged in as{pybot.user}")
channel = pybot.get_user(log_channel_id)
await channel.send('👀')
pybot.run(TOKEN, bot=True, reconnect=True)
You want to get a channel but you're using the get_user function. Since the bot can't find a user with the channel's ID, it returns None.
Replace
channel = pybot.get_user(log_channel_id)
with
channel = pybot.get_channel(log_channel_id)
#pybot.event
async def on_ready():
print(f"Logged in as{pybot.user}")
channel = pybot.get_channel(674175630916583445)
await channel.send('👀')
You can have more information about get_channel in the official Discord.py docs
get_channel(id)
I am trying to make a simple discord bot, that gives a role to a user after he gives the bot a certain command
On the command !role the user should get a role called Beta
I first tried this:
from discord_webhook import DiscordWebhook, DiscordEmbed
import discord
from discord.ext.commands import Bot
from discord.ext import commands
client = commands.Bot(command_prefix = "!")
Client = discord.Client()
#client.event
async def on_message(message):
member = message.author
if member.bot:
return
if message.attachments:
return
print(message.content)
print(str(message.author))
if "role" in message.content:
embed=discord.Embed(title="Giving role.", color=0x00ff40)
await message.channel.send(message.channel, embed=embed)
role = discord.utils.get(server.roles, name="Beta")
await client.add_roles(message.author, role)
client.run("BOT TOKEN")
But I always get the following problem:
AttributeError: 'list' object has no attribute 'roles'
Thanks a lot for taking time to read this and if you can help me. Thanks
When you do:
role = discord.utils.get(server.roles, name="Beta")
You have to use
message.guild.roles
instead of:
server.roles
to access the list of roles, newer discord versions use guild instead of server to avoid confusion with voice servers.