from discord.ext import commands
from discord.utils import get
client = discord.Client()
ROLE = 'SPECIAL'
BOT_PREFIX = '/'
bot = commands.Bot(command_prefix=BOT_PREFIX)
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message, member):
if message.author == client.user:
return
if message.author.name == "Pawlu_il_Fenku":
await message.channel.send('Hello!')
role = get(member.guild.roles, name=ROLE)
await member.add_roles(role)
print(f"{member} was given {role}")
client.run('NzY0MjA1MzA1MjQwMjIzNzQ0.X4C3pw.5RmXn1XswHCTWwOQ5i1v5lH5B6I')
when i run it and type something it gives me this:
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\jpbay\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 333, in _run_event
await coro(*args, **kwargs)
TypeError: on_message() missing 1 required positional argument: 'member'
any help?
on_message event has only 1 argument, which is message. You cannot use 2 parameters in this event but you can access every attribute that member object has with using message.author.
Also, I don't recommend you to use discord.Client and discord.Bot at the same time. discord.Bot can do everything that discord.Client does.
ROLE = 'SPECIAL'
BOT_PREFIX = '/'
client = commands.Bot(command_prefix=BOT_PREFIX)
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message, member):
if message.author == client.user:
return
if message.author.name == "Pawlu_il_Fenku":
await message.channel.send('Hello!')
role = get(message.guild.roles, name=ROLE)
await message.author.add_roles(role)
print(f"{message.author} was given {role}")
client.run('TOKEN')
NOTE:
You should change your token if you haven't changed yet.
Related
I added the bot status and after that the
Commands don't work. Added a answerers answer but it still no work ( help works not but not hello ;-;)
import discord
from KeepAlive import keep_alive
client=discord.Client()
#client.event
async def on_ready():
await client.change_presence(status=discord.Status.online,activity=discord.Game('Hey There! Do €help to start!'))
print('We have logged in as {0.user}'.format(discord.Client))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
if message.content.startswith('$help'):
await message.channel.send('no help here!')
await bot.process_commands(message)
keep_alive()
client.run('wont say token :)')
If you are talking about commands and not "commands" that you run with on_message then you have to add await client.process_commands(message) (check this issue in documentation). If your on_message event is not working then it's probably only because of missing _ in on_message event.
#client.event
async def on_message(message): # your forgot "_"
if message.author == client.user: # discord.Client won't work. Use client.user instead
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
if message.content.startswith('$help'):
await message.channel.send('no help here!')
await client.process_commands(message) # add this line at the end of your on_message event
the problem is your message function, it has async def onmessage(message): but the correct one is:
#client.event
async def on_message(message):
And I recommend defining the prefix and then separating it from the message so you don't have to keep typing $ in every if, and save the elements written after the command for future functions:
PREFIX = "$"
#client.event
async def on_message(message):
msg = message.content[1:].split(' ')
command = msg[0]
if command == "hello":
await message.channel.send('Hello!')
This seems like a popular error, but I can't for the life of me find a fix that'll work for me. I'm trying to give a member a role whenever they join my server but every time it reaches await member.add_roles(member, role) it sends off the error in the title. I've tried testing in the on_message function with the exact same results.
import os
import discord
from discord.ext import commands
from replit import db
client = discord.Client()
intents = discord.Intents.all()
client = commands.Bot('PREFIX',intents=intents)
#client.event
async def on_ready():
print("I'm in")
print(client.user)
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == "ping":
await message.channel.send("pong")
#client.event
async def on_member_join(member):
print(f"{member} has joined")
role = discord.utils.get(member.guild.roles, name="Member")
await member.add_roles(member, role)
client.run(token)
print(f"{member} has joined")
role = discord.utils.get(member.guild.roles, name="Member")
await member.add_roles(member, role) # member argument is causing issue, remove that
member is discord.Member object, if you put that in add_roles, it will error out and throw discord.NotFound 404
print(f"{member} has joined")
role = discord.utils.get(member.guild.roles, name="Member")
await member.add_roles(role)
also consider getting rid of that client = discord.Client() line, it's quite useless
I have this code that sends a message with 4 reactions, is there a way to take the first reaction a user inputs (it has to take only one input if the user chooses another option and it overrides the first one that is also fine) and save it as a variable to use later on?
import discord
import os
client = discord.Client()
some_list = []
msg = None
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
msg = await message.channel.send("**Question 1?**")
reactions = ['1️⃣','2️⃣','3️⃣','4️⃣']
for emoji in reactions:
await msg.add_reaction(emoji)
#client.event
async def on_reaction_add(reaction, user):
if reaction.message == msg:
some_list.append(user)
client.run("token")
Thanks in Advance!
You should really use wait_for an example of how to use this can be found on my previous answer: Getting input from reactions not working discord.py
You should really use a dictionary if you don't want to go with wait_for.
import discord
import os
client = discord.Client()
some_dict = {}
msg = None
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
msg = await message.channel.send("**Question 1?**")
reactions = ['1️⃣','2️⃣','3️⃣','4️⃣']
for emoji in reactions:
await msg.add_reaction(emoji)
#client.event
async def on_reaction_add(reaction, user):
if reaction.message == msg:
some_dict[user.id] = str(reaction.emoji)
client.run("token")
You can then access it using some_dict[user.id]
So basically I have this code:
import discord
import os
bot = commands.Bot(command_prefix = "!")
TOKEN = (os.getenv("TOKEN"))
client = discord.Client()
#client.event
async def on_message(message):
if message.content.startswith('!help'):
embedVar = discord.Embed(
title="Help Page", description="Under development", color=0x00ff00)
await message.channel.send(embed=embedVar)
#client.command()
#commands.has_any_role("Owner")
async def ban (ctx, member:discord.User=None, reason =None):
if member == None or member == ctx.message.author:
await ctx.channel.send("You cannot ban yourself")
return
if reason == None:
reason = "Breaking Rules"
message = f"You have been banned from {ctx.guild.name} for {reason}"
await member.send(message)
# await ctx.guild.ban(member, reason=reason)
await ctx.channel.send(f"{member} is banned!")
#client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
client.run(TOKEN)
When I run this I get the sequent error:
Traceback (most recent call last):
File "main.py", line 4, in <module>
bot = commands.Bot(command_prefix = "!")
NameError: name 'commands' is not defined
Can someone please help me?
In the documentation for commands, it states to add this to your imports:
from discord.ext import commands
commands isn't defined because it isn't imported in your code put from discord.ext import commands at the top of your code. And, you don't need discord.Client() bc commands.Bot() is a subclass of it so please remove it for it is redundant. and finally, change client.command to bot.command to make the code work.
from discord.ext import commands
load_dotenv("token.env")
token = os.getenv("Token")
bot = commans.Bot(command_prefix="!")
#bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content.startswith("!"):
await bot.process_commands(message)
return
else:
author = message.author
await message.channel.send(message.content)
How can i change the code in line 15 to send the message to "author"?
You can use Member.create_dm() for this. You can modify your code as follows:
from discord.ext import commands
load_dotenv("token.env")
token = os.getenv("Token")
bot = commands.Bot(command_prefix="!")
#bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content.startswith("!"):
await bot.process_commands(message)
return
else:
c = await message.author.create_dm()
await c.send(message.content)
You also misspelt commands in bot = commands.Bot(... :)
You could use await ctx.author.send() so that whoever runs the command would get it sent in there dms
from discord.ext import commands
load_dotenv("token.env")
token = os.getenv("Token")
bot = commands.Bot(command_prefix="!")
#bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content.startswith("!"):
await bot.process_commands(message)
return
else:
author = message.author
await ctx.author.send(message.content)