What am I doing wrong?
#bot.event
async def on_member_join(member):
print(f"{member} join")
role_1 = member.guild.get_role(start_role_id)
await member.add_roles(role_1)
I searched for answers on the forums for a long time and nothing helped.
Did you enable intents in Discord Developer Portal? while initializing bot add intents=discord.Intents.all(). Also I fixed your code.
bot = commands.Bot(command_prefix='', intents=discord.Intents.all())
#bot.event
async def on_member_join(member):
print(f"{member} join")
role1 = discord.utils.get(member.server.roles, id=role_id)
await member.add_roles(role1)
Try this:
bot = commands.Bot(command_prefix='PREFIX_HERE',
intents=discord.Intents.all())
#bot.event
async def on_member_join(member):
print(f"{member} has joined")
my_role = discord.utils.get(member.guild.roles, id=role_id)
await member.add_roles(my_role)
Related
I'm using discord.py to write a bot. I want it to add a role named 'unverified' to all members upon joining. I don't get any error but the role just doesn't get added for some reason
#client.event
async def on_member_join(member):
role_unverified = discord.utils.get(member.guild.roles, name="unverified")
await member.add_roles(role_unverified)
from discord.utils import get
#client.event
async def on_member_join(member):
role = get(member.guild.roles, name="unverified")
await member.add_roles(role)
this should work i think
aren't you retrieving the role from the member you're trying to assign it to?
Woulnd't it be something like
#client.event
async def on_member_join(member):
role_unverified = discord.utils.get(guild.roles, name="unverified")
await member.add_roles(role_unverified)
The code essentially is supposed to create an embed and for some reason it gives me the error " 'message' is not defined" (the embed will be put into anoter bot)
import discord
from discord.ext import commands
import random
from discord.ext.commands import Bot
class MyClient(discord.Client):
async def on_ready(self):
print('Logged on as', self.user)
channel = self.get_channel(717005397655027805)
await channel.send("I am now online")
messageContent = message.Content
if message.author == self.user:
return
#client.event
async def on_message(message):
if message.content.startswith('!hello'):
embedVar = discord.Embed(
title="Title", description="Desc", color=0x336EFF
)
embedVar.add_field(name="Field1", value="hi", inline=False)
embedVar.add_field(name="Field2", value="hi2", inline=False)
client.send_message(message.channel, embed=embedVar)
PREFIX = ("$")
bot = commands.Bot(command_prefix=PREFIX, description='Hi')
client = MyClient()
client.run('TOKEN')
As you use CLASSES AND OBJECTS or OOP for the bot you need correct syntax too. I can't really help you with than but can use normal way.
Step 1:
We will import the libraries:
import discord
from discord.ext import commands
import random
Step 2:
We will define the Prefix and Bot's variable.
PREFIX = "$"
bot = commands.Bot(command_prefix=PREFIX, description="Hi")
Step 3:
We will now write the bot's on_ready() command according to your code.
#bot.event
async def on_ready():
print('Logged on as', self.user)
channel = bot.get_channel(717005397655027805)
await channel.send("I am now online")
The statement bot.get_channel() is being used to get the channel to send the message.
Step 4:
We will now write bot's on_message function according to your code.
#bot.event
async def on_message(message):
if message.content.startswith('!hello'):
embedVar = discord.Embed(
title="Title", description="Desc", color=0x336EFF
)
embedVar.add_field(name="Field1", value="hi", inline=False)
embedVar.add_field(name="Field2", value="hi2", inline=False)
await message.channel.send(embed=embedVar)
Step 5:
We will now create a statement to start up your bot.
bot.run("TOKEN")
Add this command at the bottom of the script.
The whole code compiled will be:
import discord
from discord.ext import commands
import random
PREFIX = "$"
bot = commands.Bot(command_prefix=PREFIX, description="Hi")
#bot.event
async def on_ready():
print('Logged on as', self.user)
channel = bot.get_channel(717005397655027805)
await channel.send("I am now online")
#bot.event
async def on_message(message):
if message.content.startswith('!hello'):
embedVar = discord.Embed(
title="Title", description="Desc", color=0x336EFF
)
embedVar.add_field(name="Field1", value="hi", inline=False)
embedVar.add_field(name="Field2", value="hi2", inline=False)
await message.channel.send(embed=embedVar)
This would solve your problem. If you still get any problem, please ask me in the comments. :)
Thank You! :D
Try this (based of Bhavyadeep's code):
import discord
from discord.ext import commands
import random
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")
#bot.event
async def on_message(ctx):
if ctx.content.startswith('!hello'):
embedVar = discord.Embed(
title="Title", description="Desc", color=0x336EFF
)
embedVar.add_field(name="Field1", value="hi", inline=False)
embedVar.add_field(name="Field2", value="hi2", inline=False)
await ctx.channel.send(embed=embedVar)
bot.run("TOKEN")
I've simply replaced "message" with "ctx". If this still doesn't work, then try replacing bot = commands.Bot(command_prefix=PREFIX, description="Hi") with bot = commands.Bot(command_prefix=PREFIX, description="Hi", pass_context=True).
It also appears your indentation in the class MyClient is wrong, so fix that first. Make sure that async def on_message(... is in the class scope.
If all fails, try and move the code to the global scope (delete the class).
You didn't specify what message.Content is in on the on_ready() function, also you need to create the on_message() function outside of the on_ready() func, and you didn't specify what the #client decorator is, you just specified it at the end of your code.
All in all, your fixed code should look something like this
from discord.ext import commands
TOKEN = "Your token here"
PREFIX = '$'
bot = commands.Bot(command_prefix=PREFIX)
#bot.event
async def on_ready():
print('Logged on as', bot.user)
channel = bot.get_channel(717005397655027805)
await channel.send("I am now online")
#bot.command() # Usage - $hello
async def hello(ctx):
if ctx.message.author == bot.user: return
embedVar = discord.Embed(title="Title", description="Desc", color=0x336EFF)
embedVar.add_field(name="Field1", value="hi", inline=False)
embedVar.add_field(name="Field2", value="hi2", inline=False)
await ctx.send(embed=embedVar)
bot.run(TOKEN)
I'm trying to use bot commands in discord so I can start using the bot prefix. I tried using ctx, but from the code that I have written, when I use .ping, it returns nothing. Anyone got any idea why?
import discord
from discord.ext import commands
client = discord.Client()
bot = commands.Bot(command_prefix= ".")
#client.event
async def on_ready():
print("Logged in") #login message
#bot.command(pass_context = True) #trying to get "test" to return from .ping
async def ping(ctx):
await ctx.channel.send("test")
#client.event
async def on_message(message):
if message.author == client.user: #testing the bot actually responds to something
return
if message.content.startswith("Hello"):
await message.channel.send("Hi")
client.run('#bot token here')
There are a lot of things wrong in your code:
Use discord.Client or commands.Bot not both
You didn't enable intents, so message.author is None, here's how:
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(..., intents=intents)
Also make sure to enable privileged intents in the developer portal
You're running the client, not the bot, if you want the commands to work run the bot and get rid of client
Change the decorator of the on_message and on_ready events and add process_commands at the end of on_message:
#bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content.startswith("Hello"):
await message.channel.send("Hi")
await bot.process_commands(message)
Here's your fixed code:
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix= ".", intents=intents)
#bot.event
async def on_ready():
print("Logged in")
#bot.command()
async def ping(ctx):
await ctx.send("test")
#bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content.startswith("Hello"):
await message.channel.send("Hi")
await bot.process_commands(message)
bot.run('#bot token here')
There are some bugs in your code that I have found.
Don’t use both discord.Client() and commands.Bot()
You need to enable intents. Go to the discord developers page then to your bot and enable both intents. Quick Link To The Developer Page On Discord
Then write this code:
intents = discord.Intents.all()
bot = commands.Bot(commands_prefix=“.”, intents=intents)
Make sure you are not using any client here.
There are even more stuff needed to fix. Since now we are using bot, change everything saying client to bot. You also have to add await bot.process_commands(message) to the end of the on_message function. Also change ctx.channel.send to just ctx.send
#bot.event
async def on_ready():
print("Logged in") #login message
#bot.command(pass_context = True) #trying to get "test" to return from .ping
async def ping(ctx):
await ctx.channel.send("test")
#bot.event
async def on_message(message):
if message.author == bot.user: #testing the bot actually responds to something
return
if message.content.startswith("Hello"):
await message.channel.send("Hi")
await bot.process_commands(message)
bot.run('#bot token here')
Just use simple ctx.send, it's common for commands,
#bot.command()
async def ping(ctx):
await ctx.send("test")
I have a very juvenile bot, and I want to add an automod feature. However, my bot is written in discord.ext.commands, and to scan all the messages, I would need discord.Client (I think). I'm not sure if they can both be run at the same time, but are they compatible?
You don't need discord.Client. If you are already using discord.ext.commands, you can do it as follows.
import discord
from discord.ext import commands
intents = discord.Intents.all()
bot = commands.Bot(command_prefix=".", intents=intents)
#bot.event
async def on_ready():
print(f"Bot is ready")
await bot.change_presence(status=discord.Status.online, activity=discord.Game(name="example"))
#bot.event
async def on_message(message):
forbidden_word = "word"
if forbidden_word in message.content:
await message.delete()
#bot.command()
#commands.has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member):
await member.kick()
await ctx.send(f"Member {member} has been kicked")
bot.run("token")
on_message is called when a Message is created and sent.
discord.Intents.messages must be enabled or you use intents = discord.Intents.all() to enable all Intents
so I'm working on making my own bot for my server and after a while I finally found a string for autorole & role assignment that worked.
I then kept on adding another string for the bot simply replying "Hello". As soon as I add that the role commands won't work anymore. Once I take it out it works again.
On the other hand I have a 8ball and a dice roll command that works with and without the Hello Command
I have no idea what is the problem...
#client.event
async def on_member_join(member):
channel = discord.utils.get(member.guild.channels, name='entrance')
await channel.send(f'Welcome {member.mention} to Dreamy Castle! \n Please make sure to read the rules!')
role = discord.utils.get(member.guild.roles, name="Peasants")
await member.add_roles(role)
#client.event
async def on_message(message):
if message.content.startswith('+acceptrules'):
member = message.author
role1 = discord.utils.get(member.guild.roles, name='The People')
await member.add_roles(role1)
#client.event #this is the hello command
async def on_message(message):
message.content.lower()
if message.content.startswith('Hello Conny'):
await message.channel.send('Hello!')
Use if and elif not 2 different functions for same event.
Also you might need commands.Bot for a fully functional commanded bot.
#client.event
async def on_message(message):
if message.content.startswith('+acceptrules'):
member = message.author
role1 = discord.utils.get(member.guild.roles, name='The People')
await member.add_roles(role1)
elif message.content.lower().startswith("hello conny"):
await message.channel.send("Hello!")
await client.process_commands(message)