When I run the python code below, it doesn't pick up messages from other bots:
#bot.event
async def on_message(message):
print(message)
Is there any way to make so that my discord.py bot does pick up messages from other bots?
Discord.py bots are set to ignore messages sent by other bots, see the code here - more specifically lines 972 and 973:
if message.author.bot:
return
To work around this, you can subclass the bot, and override the process_commands method as shown below:
class UnfilteredBot(commands.Bot):
"""An overridden version of the Bot class that will listen to other bots."""
async def process_commands(self, message):
"""Override process_commands to listen to bots."""
ctx = await self.get_context(message)
await self.invoke(ctx)
and run your code using this bot instead. Probably not the best to use in production, but is a good way to facilitate testing of your bot via another bot
I decided to just use the channel.history(limit=10).flatten() and channel.fetch_message(ID) functions and put them in a loop, which also works well for my application.
As the messages are from bots, could it be because the bots are using embeds? Because discord cannot print message from embeds(Maybe unless if you use message.embeds) Check if the messages the bots are sending are plain text instead of embeds
Related
I can't get it to work through properties like command or just content_types. No errors show up.
#dp.message_handler(commands=["start"])
async def start(msg:Message):
if msg.chat.type == ChatType.CHANNEL:
await msg.answer(msg)
I made so bot sent a message to the channel. Still nothing.
P.S It works with getUpdates
In the Dispatcher class there is a decorator called "channel_post_handler".
I'm creating a simple Twitch bot for personal use. I'm using twitchio.ext commands. Everything works fine, I'm connected, I'm able to print all the messages from the chat, my bot responds to commands but I'm not able to send a message to chat and I don't know why.
from twitchio.ext import commands
class Bot(commands.Bot):
def __init__(self):
super().__init__(token='oauth:censored', prefix='g ', nick = "nick of the bot", irc_token = "censored", initial_channels=['channel'])
async def event_ready(self):
print(f'Using bot as {self.nick}')
#commands.command()
async def test(self, ctx: commands.Context):
await ctx.send('test')
print("printed")
bot = Bot()
#bot.event()
async def event_message(msg):
print(msg.author.name)
print(msg.content)
await bot.handle_commands(msg)
bot.run()
When I type "g test" in chat, the message "test" is not sent but the message "printed" is printed to the console. Do you know where could be the problem? Also, I would like to ask if there is any way how to send a message to chat directly without responding to a command or event.
Try adding the bot as a mod
The 'nick' in the bot init() function doesn't seem to do anything the nick is linked to your IRC username.
I'm assuming the channel in your initial_channels in the init() func isn't actually the channel you put there since you said you were able to type in the chat.
If the channel that you are using the bot from(linked to the oauth key) is not the same channel as you are connecting to in the chat then you probably need to add the bots username as a moderator in that chat
try
/mod [bot_username]
in the chat as the channel owner to do this
Also if your bot is the same account you are sending commands from on twitch the rate limit will be reached unless they are a mod. So either make a dedicated account for your bot or mod the bot on the server
I'm trying to simply create a function that sends a discord message to a specific channel when the bot runs. I want to later call this function in a separate python script.
The code below seems to run fine, and the bot appears online - but it is fails to send the message:
import discord
client = discord.Client()
client.run("ABCDeFGHIjklMNOP.QrSTuV-HIjklMNOP") # the token
#client.event
async def on_ready():
logs_channel = client.get_channel(12345689123456789) # the channel ID
await logs_channel.send("Bot is up and running")
on_ready()
I'm sure I'm missing something basic but I cannot figure out what it is. I've tried a similar function in Node.js and it works fine however, I would prefer the bot to work in python. Discord.py is up to date, python is v3.10.
Any help would be greatly appreciated.
Client.run is a blocking call, meaning that events registered after you call it won't actually be registered. Try calling Client.run after you register the event, as in:
import discord
client = discord.Client()
#client.event
async def on_ready():
logs_channel = client.get_channel(12345689123456789) # the channel ID
await logs_channel.send("Bot is up and running")
client.run("ABCDeFGHIjklMNOP.QrSTuV-HIjklMNOP") # the token
You also probably do not want to be manually calling an event handler. If you want to send a message by calling a function, you probably want to consider writing a separate function to the event handler.
I'm trying to figure out how to write a simple bot for Discord.
However, the problem I'm facing is that for one particular server (or guild as it is called), I don't get any messages.
I am logged in this guild in the webinterface with the same user. And I see messages coming in.
However, in the client I do not see anything being logged.
The code is very simple:
import discord
class DiscordClient(discord.Client):
async def on_ready(self, *args, **kwargs):
print(f"Connected as {self.user.display_name} to:")
for guild in self.guilds:
print(f"- {guild.name}")
async def on_message(self, message):
print(f"Message from '{message.guild.name}'")
DiscordClient().run(TOKEN, bot=False)
In the on_ready, I can see it knows the guild and even the channels. So that's fine. There is a connection...
The server is the "Among Us" server which has 500k users. So maybe it's a limitation because too much users? As the on_message does trigger correctly for servers with much more limited amount of users (like 20k or so).
What setting should I adjust to connect to this server in order to receive the messages?
Thanks!
I don't see any issue with your code tbh, maybe it's an intents issue (?), try enabling some basic intents
intents = discord.Intents.default()
DiscordClient(intents=intents).run(TOKEN, bot=False)
I want to send a private message to a specific user with my discord bot. I am using discord==1.0.1
discord.py==1.3.1. I already tried using the docs (https://discordpy.readthedocs.io/en/latest/api.html?highlight=private%20message#discord.DMChannel) but i dont understand that. I tried following code, which didnt worked:
#client.command()
async def cmds(ctx):
embed = discord.Embed(title='Discord Server Befehle',
description='Description',
color=0x374883)
[...]
msg = await ctx.send(embed=embed)
await ctx.author.create_dm('Hi')
print(f'{command_prefix}help ausgeführt von {ctx.author}')
Try using the send function instead of the create_dm. create_dm only creates the channel between two users which discord does automatically as far as i know.
according to documentation
This should be rarely called, as this is done transparently for most people.
so it should be
ctx.author.send('Hi')