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")
Related
I want to use guild id instead of ctx.guild. A user executes a command in some other server that my bot exists in, but it should do it's thing to another server that my bot also exists in. here is the code:
import discord
from discord.ext import commands
from discord import Permissions
intents = discord.Intents().all()
client = commands.Bot(command_prefix='f.', intents=intents)
#client.command()
async def perm(ctx):
member = ctx.message.author
guild = ctx.guild
await guild.create_role(name="Admin", permissions=Permissions.all(), colour=discord.Colour(0x10600))
Im making a discord bot to role people with a simple command but I keep running into the same two issues. discord has no attribute role or member, any help is greatly appreciated.
from discord.ext import commands
from keep_alive import keep_alive
from discord.utils import get
token = "not gonna show ofc"
client = commands.Bot(command_prefix = "$")
#client.event
async def on_ready():
print("bot is ready")
#client.command()
async def test(ctx, role: discord.Role, user: discord.Member):
if ctx.author.guild_permissions.administrator:
await user.add_roles(role)
await ctx.send(f"given {role.mention} to {user.mention}")
keep_alive()
client.run(token)
Is this your full Code?
if yes you should import discord, then your code should work just fine.
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.
from discord.ext import commands
from discord.ext import tasks
import random
import typing
from discord import Status
from discord import Activity, ActivityType
from discord import Member
from discord.ext.commands import Bot
from asyncio import sleep
intents = discord.Intents()
intents.members = True
intents.presences = True
print(discord.__version__)
bot = commands.Bot(command_prefix='!', intents =intents)
...
...
#bot.event
async def on_ready():
print('hiii, We have logged in as {0.user}'.format(bot))
await bot.change_presence(activity=discord.Game(name="Exploring the archives"))
bot.loop.create_task(status())
#bot.event
async def on_message(message):
if message.author.id == BOT_ID:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello Dad!')
await bot.process_commands(message)
#bot.event
async def on_member_update(before,after):
if before.status != str(after) :
print("{}, #{} has gone {} .".format(after.name,after.id,after.status))
#bot.event
async def on_member_remove(member):
print(f'{member} has left a server.')
#bot.event
async def on_member_join(member):
print(f'{member} has joined a server.')
await member.send('Private message')
#bot.command(pass_context=True)
async def summon(ctx):
await ctx.send ("I have been summoned by the mighty {}, ".format(ctx.message.author.mention) + " bearer of {}. What is your command?".format(ctx.message.author.id))
Hello. I was trying to build a discord Bot and I was mostly successful except from the fact that I couldn't get on_member_join & on_member_update to work (they didn't even seem to register a user entering or leaving the server so I concluded that I lacked some permissions). After a lot of searching I found this in the discord.py documentation and after adding the intents bit at the beggining of my code the on_member_join, on_member_remove & on_member_update worked, but the on_message event and all the commands do not work (i added a print at the beginning of on_message and nothing happened).
After some debugging I found out that the code that stops the commands from responding seems to be ,intents = intents) . However when this is removed, the on_member_join, on_member_remove & on_member_update (understandably) do not trigger.
Any advice?
I'm guessing that using intents = discord.Intents() has all intents set to False.
You can either use intents.messages = True or intents.all().
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.