Discord Python Bot only reacts on DM message - python

Dear Community I tried my best but I cant find out why the bot only reacts on dm messages. Yesterday evening the bot worked fin and reacted to messages in a Discord but now he only reacts to dm´s. This code is just a short example of the on_message function I´m using but I cant find an alternative to on_message.
from timeit import repeat
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
from datetime import datetime
import time
import sqlite3
from discord.message import Message
bot = commands.Bot(command_prefix='!')
PREFIX = '!'
#bot.event
async def on_message(message):
if message.content.startswith(PREFIX):
if message.content == (PREFIX + "test"):
await message.channel.send("test")
#bot.event
async def on_ready():
print("GuMo!")
bot.run("Token")```

I think that this probably has to do with the "intents" permissions that are being added to discord bots. Try
bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())
and also give the bot the message content intent on the developer portal https://discord.com/developers/applications

If you install this:
pip install -U git+https://github.com/Rapptz/discord.py
Your bot will only react to messages send as DM.

Related

What is wrong with my code that sends a message when a discord bot joins a server?

I am trying to make a discord bot send the message like "hi guys" with discord.py whenever it joins a server. but the code that I am trying to use to send the message does not work. It doesn't show any errors however.
code: (some of the imports are for other stuff in the code)
import discord
import os
import random
import time
from discord import channel
from discord.message import DeletedReferencedMessage, Message
from discord.utils import find
client = discord.Client()
#client.event
async def on_member_join(member):
if member == client.user:
await channel.send('hi guys')
Please tell me what is wrong so that I can get it working. (I am fairly new to coding in python btw)
Change on_member_join to on_guild_join.
on_member_join: called when a member joins the guild
on_guild_join: called when the bot joins a guild

Discord bot to send text messages with image from built in command (Python; Can redo in another language)

I've been working on trying to build a discord bot for a server that you could for example do "!publish [text + upload]" this would then send whatever you've typed (plus the uploaded image) from the bot in the current channel. I had had sqiggly code I was doing in Python off a new tutorials but so far nothing has stuck.
What I've got below is an attempt at having it send an image from the bot on command as a start however it hasn't even functioned this far. If anyone would be able to help me fix this up to get the desired result that would be great, I'm happy to switch up languages if that's easier. So far I've got it repeating text from command (See: $hello the bot will send hello! back. I'll be keeping that command for further use but haven't been able to get anything with an image extension). Have tried both file images and url; tried all the options on discord documentation without success.
import discord
import os
from replit import db
from discord.ext import commands
from discord.ext.commands import bot
import asyncio
import datetime as dt
client = discord.Client()
bot = commands.Bot(command_prefix='!')
#bot.event
async def on_ready():
print('Connected!')
#client.event
async def on_ready():
print('Hello world {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!')
#bot.command(pass_context=True)
async def test(ctx):
await ctx.send('Working!', file=discord.File('image.png'))
client.run(os.environ['TOKEN'])
First tutorial I followed had me using replit to code this, store data and keep it cloud-base operational even when the computer wasn't active. The bot is functioning at this time just using the $hello command and anything similar. Nothing with images.
First of all you should only use either client or Bot not both, bot is the better option since it has commands and events. here is your code organized using commands.Bot(command_prefix='!')
import discord
import os
from replit import db
from discord.ext import commands
import asyncio
import datetime as dt
bot = commands.Bot(command_prefix='!')
#bot.event
async def on_ready():
print(f'Connected! {bot.user}')
#bot.event
async def on_message(message):
if message.author == bot.user:
return
#code here
# !hello
#bot.command()
async def hello(ctx):
await ctx.send('Hey!')
# !test
#bot.command()
async def test(ctx):
await ctx.send('Working!', file=discord.File('image.png'))
bot.run(os.environ['TOKEN'])
You have defined two instances and only ran client. That is why !test did not work

Having Trouble With Discord.py Events

I have been using Python for bot development, but recently i faced some problems with events that does not trigger when the action happen.
events like : on_member_remove #I tried both# , on_guild_role_delete , on_guild_channel_delete
I want to know why they don't work , if you know why any of them does not work i will appreciate your help
from discord.ext.commands import Bot, Greedy
from discord import user
import discord
from discord.ext import commands
from discord.ext.commands import Bot, has_permissions, MissingPermissions
import asyncio
intents = discord.Intents()
intents.members = True
bot = commands.Bot(command_prefix="/", help_command=None, intents=intents)
#bot.event
async def on_ready():
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="TEST"))
print('Hello :|')
#bot.event
async def on_member_remove(member):
print("on_member_kick Worked")
#bot.event
async def on_guild_role_delete(role):
print("on_role_delete Worked")
#bot.event
async def on_guild_channel_delete(channel):
print("on_channel_delete Worked")
Oh dear, this can be fixed if you look at the docs
The on_member_kick, on_role_delete and on_channel_delete events simply do not exist.
Possible alternatives are:
on_member_remove (which should've worked according to the docs)
on_guild_channel_delete
on_guild_role_delete
Also, check that you have enabled the intents on the developers page, it may be the cause for the on_member_remove event to not work.

Python discord bot closes/crashes immediately after opening

I'm trying to make a discord bot with discord.py for my discord server, I have extremely basic code that should get the bot online but instead just opens and then crashes.
import discord
#client.event
asnyc def on_ready():
print 'bot read`enter code here`y'
client.run('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
I do have the token normally I just didn't want to leak. I appreciate all the help.
I found a nice tutorial to create a discord bot in python. Therefore you can create a connection with the following code:
import os
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
client = discord.Client()
#client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
client.run(TOKEN)
And I would suggest to rename the file from 'discord' to 'bot' or something else, so there are no conflicts with the import.

How to make a discord bot react to PAST private messages?

I made a simple discord.py bot that reacts whenver it is PM'd. However, if someone messages it while my bot is offline, it will not react. How can i make it display all messages received while it was online on startup?
Current code:
import discord
from discord.ext import commands
import asyncio
client = commands.Bot(command_prefix='.')
#client.event
async def on_message(message):
if message.guild is None and message.author != client.user:
send_this_console=f'{message.author}: \n{message.content}\n'
print(send_this_console)
await client.process_commands(message)
You'll need to use message history and track the last message sent.
To get when a message was created:
lastMessageTimestamp = message.created_at
You'll need to store this in whatever way you want when your bot is offline (for example with pickle) and do this:
async for message in user.history(after = lastMessageTimestamp):
print(message.content)

Categories