I'm a beginner to python and I am following a YouTube tutorial, and the problem is that the bot would not respond to any of the messages. Also I'm trying this on Replit. I would really appreciate your help.
import discord
import os
client = discord.Client(intents = discord.Intents.default())
#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
if message.content.startswith('$hello'):
await message.channel.send('hello')
client.run(os.environ['TOKEN'])
You’re only using the default intents: you haven’t set the message content intents. See these for more info:
https://support-dev.discord.com/hc/en-us/articles/4404772028055-Message-Content-Privileged-Intent-FAQ
https://discordpy.readthedocs.io/en/stable/intents.html
Related
I have tried to search the web for ways to do this, but I've had no luck. I know this is seems very easy, but I am just starting with discord.py and I'm looking for some help. Thanks.
You can add this to your bot code.
#client.event
async def on_message(message):
if message.content == ‘verify’:
await message.channel.send(“A message”)
Here
#client.event
async def on_message(message):
if message.content == "verify":
#Your code
import discord
token = '<insert-token-here>'
# Make sure to enable the Message Content intent in the Discord developer portal
client = discord.Client(intents=discord.Intents.all())
#client.event
async def on_ready():
print(f"Logged on as {client.user}")
#client.event
async def on_message(message: discord.Message):
if message.author == client.user:
# skip message from self
return
print(f"Message: {message.content}")
await message.channel.send(message.content)
if __name__ == '__main__':
client.run(token)
import discord
client = discord.Client()
#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
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
client.run('token is here')
I'm trying to create a discord bot and I'm trying to use this simple code to get started but whenever I type the command ($hello) in a server that the bot is in it doesn't respond. Any solutions?
I'm assuming you're new to discord.py which is fine, though writing code like this is a little counter-intuitive. Instead of registering it as an event, why not just register it as an actual command?
This is how it should be done:
from discord.ext import commands
client = commands.Bot(command_prefix='$') # This can always be changed
#client.event()
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.command()
async def hello():
await ctx.send('Hello')
client.run('token is here')
So, basically i was trying to make a bot for discord using python and this is my first project so i was trying out new stuffs
here's my code
import discord
from http import client
from discord.ext import commands
client = discord.Client()
client = commands.Bot(command_prefix='`')
#client.event
async def on_ready():
print("Bot is online")
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == 'hello':
await message.channel.send('Welcome to the server, human')
#client.command
async def info(ctx):
await ctx.send(ctx.guild)
client.run(#mytokenishereicantshareit)
as you can see i am completely new to programming in general, so if you may help me out, the bot is saying "Bot is online" in output and it's getting online in my server its not showing any errors either. but it's none of my commands are working, like the "hello" and `info.
Edit : This issue has been fixed, There are two possible solutions for this either you can replace the #client.event with #client.listen or just add a await bot.process_commands(message) after
if message.content == 'hello':
await message.channel.send('Welcome to the server, human')
Part like
if message.content == 'hello':
await message.channel.send('Welcome to the server, human')
await bot.process_commands(message)
and you're done.
Firstly, remove the client = discord.Client() bit because the bot already does that, and secondly, add a bracket after the #client.command so it's #client.command()
I'm new to coding discord bots, so this might be some easy fix mistake I made but when a member joins my server, the bot doesn't seem to react, be it to add a role or just send a random message in the console.
I'd also add, I'm using replit to host my bot.
import os
import discord
from discord.ext import commands
from replit import db
client = discord.Client()
#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("A member has joined")
role = discord.utils.get(member.server.roles, id="920344130184437931")
await client.add_roles(member, role)
You need to enable Privileged Gateway Intents in Discord Developer Portal.
When you enable that, you need to type this in your code:
intents = discord.Intents.all()
client = commands.Bot('PREFIX',intents=intents)
#client.event
async def on_ready():
print("I'm in")
....
After that on_member_join event should work
Im new to python so I cant see where I have made an error.
import discord
import os
from config import TOKEN
client = discord.Client()
#Sends a message in terminal if it has worked
#client.event
async def on_ready():
print('We have logged in as {0.user}'
.format(client))
#Sends a message if !test is included in a sent message
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!test'):
await message.channel.send('test')
#client.event
async def on_member_join(self, member):
guild = member.guild
if guild.system_channel is not None:
to_send = 'Welcome {0.mention} to {1.name}'.format(member, guild)
await guild.system_channel.send(to_send)
client.run(TOKEN)