Discord bot copy messages from server to my server - python

it's possible to copy new messages from X server Y channel to my X server Y channel, using bot in Discord?
the server from which I want to copy the message is the official game server (on Discord) where I connect as a user, and my server is the guild server for this game where I want to send news and update announcements.
sorry for my English

This certainly is possible, using the on_message() event:
#bot.event
async def on_message(message):
await bot.process_commands(message) # add this if also using cmd decorators
if message.channel.id == THEIR_CHANNEL_ID_HERE:
target_channel = bot.get_channel(YOUR_ANNOUNCEMENT_CHANNEL_ID_HERE)
await target_channel.send(message.content)
And of course you can add checks for file attachments, specific users, etc if you want to.
References:
Bot.process_commands()
TextChannel.id
Client.get_channel()
Message.attachments
Member.id

Related

how to make a discord.py bot not accepts commands from dms

How do I make a discord.py bot not react to commands from the bot's DMs? I only want the bot to respond to messages if they are on a specific channel on a specific server.
If you wanted to only respond to messages on a specific channel and you know the name of the channel, you could do this:
channel = discord.utils.get(ctx.guild.channels, name="channel name")
channel_id = channel.id
Then you would check if the id matched the one channel you wanted it to be in. To get a channel or server's id, you need to enable discord developer mode. After than you could just right click on the server or channel and copy the id.
To get a server's id you need to add this piece of code as a command:
#client.command(pass_context=True)
async def getguild(ctx):
id = ctx.message.guild.id # the guild is the server
# do something with the id (print it out)
After you get the server id, you can delete the method.
And to check if a message is sent by a person or a bot, you could do this in the on_message method:
def on_message(self, message):
if (message.author.bot):
# is a bot
pass
You Can Use Simplest And Best Way
#bot.command()
async def check(ctx):
if not isinstance(ctx.channel, discord.channel.DMChannel):
Your Work...
So just to make the bot not respond to DMs, add this code after each command:
if message.guild:
# Message comes from a server.
else:
# Message comes from a DM.
This makes it better to separate DM from server messages. You just now have to move the "await message.channel.send" function.
I assume that you are asking for a bot that only listens to your commands. Well, in that case, you can create a check to see if the message is sent by you or not. It can be done using,
#client.event
async def on_message(message):
if message.author.id == <#your user id>:
await message.channel.send('message detected')
...#your code

How can I delete messages sent by a specific user within a specific channel using Discord.py

Hopefully this isn't a duplicate post but I've searched around and can't find anything on how to do this specifically.
I'm trying to create a Discord bot which can delete messages sent by a specific author, in a specific channel, but also checks previously sent messages.
At the moment I have the below, which adds new messages from chosen user to a list, checks if they're duplicates and if they are, deletes them.
I want to know:
A: How can I make this bot Channel specific
B: Can I have this program check old messages in that channel too and also delete them if they're duplicates?
Thanks in advance for any help and if any further info required, please let me know.
import discord
TOKEN = ('MY_TOKEN_LIVES_HERE')
client = discord.Client()
messagesSeen = []
#client.event
async def on_message(message):
if "News_Bot" in str(message.author):
if message.content in messagesSeen:
await message.delete()
else:
messagesSeen.append(message.content)
client.run(TOKEN)
Yes you can do both of those things. Since you are using on_message event you can just use message.channel to also check that channel for previously sent messages. I would also advise to change news_bot to the ID of the bot. The thing is one you implement this and clear the channel history for those messages, it won't be necessary anymore as the bot will catch the messages as soon as they are sent. So the message history is kind of a one time thing, which you can just create a cmd and run it in that channel.
messagesSeen = []
#client.event
async def on_message(message):
if 1234345 is message.author.id:
if message.content in messagesSeen:
await message.delete()
async for message in message.channel.history(limit=100):
if message.content in messagesSeen:
await message.delete()
else:
messagesSeen.append(message.content)

Welcome Message not sending to correct server. Discord.py

So, I am using this code below to send welcome & leave messages to my server.
I am going to provide an example of this error.
I have Server 1 (My main server I set the bot up on)
And then I have Server 2.
When I added my bot to Server 2, and invited a profile. It sent the welcome message to Server 1 rather than server 2.
What would I need to do with my code to make it so that when people join Server 2, it sends a welcome message to Server 2 instead of Server 1?
I realize that my Welcome & leave client events run off of the channel ID of Server 1 which is why when someone joins server 2, it sends a notification to server 1. If I wanted to let other servers use my bot, how could I make it to where the code grabs the channel ID of the server that it is being used in, instead of the notifications going all to one server?
I hope that this made sense, any help would be greatly appreciated!
#client.event #
async def on_member_join(member): #
guild = client.get_guild(919401083057618945) #
channel = guild.get_channel(919401083711934536) #
await channel.send(f'**Welcome to the server {member.mention} ! :partying_face:**') #
await member.send(f'**Welcome to the {guild.name} server, {member.name}! :partying_face:**') #
#
# (Notifies a member left) #
#client.event #
async def on_member_remove(member): #
guild = client.get_guild(919401083057618945) #
channel = guild.get_channel(919401083711934536) #
await channel.send(f'**{member.mention} Has left the server! :cry:**')```
There are a number of ways to achieve this, so I will give you two of the possible options you should choose to do.
Option 1 involves the discord.utils import. This import allows you to quickly and easily find a channel with a specific name, such as 'welcome' or 'join-and-leave'. While this is a quick and easy method, it does not allow the administrators or mods of other servers to change the channel name without this join function breaking. Do view the code below:
import discord.utils # I recommend placing this with your other imports
#client.event
async def on_member_join(member):
channel = discord.utils.get(member.guild.channels, name='welcome')
await channel.send(f"Welcome {member.mention}!")
Option 2 makes use of json, which is good when starting out on a welcome bot or the likes. There are multiple questions on doing something like this. Here are some links to help you with this option:
How to set a welcome channel discord.py? - SO
Discord.py welcome message for multiple servers - SO

python discord.py send DM to inviter on join guild

I currently have the following on_guild_join code:
#client.event
async def on_guild_join(guild):
embed = discord.Embed(title='Eric Bot', color=0xaa0000)
embed.add_field(name="What's up everyone? I am **Eric Bot**.", value='\nTry typing `/help` to get started.', inline=False)
embed.set_footer(text='Thanks for adding Eric Bot to your server!')
await guild.system_channel.send(embed=embed)
print(f'{c.bgreen}>>> {c.bdarkred}[GUILD JOINED] {c.black}ID: {guild.id} Name: {guild.name}{c.bgreen} <<<\n{c.darkwhite}Total Guilds: {len(client.guilds)}{c.end}')
(Ignore the c.color stuff, it's my formatting on the console)
It sends an embed with a bit of information to the system channel whenever someone adds the bot to a guild.
I want it to send a DM to whoever invited the bot (the account that used the oauth authorize link) the same message. The problem is that the on_guild_join event only takes 1 argument, guild, which does not give you any information about the person who used the authorize link to add the bot to the guild.
Is there a way to do this? Do I have to use a "cheat" method like having a custom website that logs the account that uses the invite?
Since bots aren't "invited", there's instead an audit log event for when a bot is added. This lets you iterate through the logs matching specific criteria.
If your bot has access to the audit logs, you can search for a bot_add event:
#client.event
async def on_guild_join(guild):
bot_entry = await guild.audit_logs(action=discord.AuditLogAction.bot_add).flatten()
await bot_entry[0].user.send("Hello! Thanks for inviting me!")
And if you wish to double-check the bot's ID against your own:
#client.event
async def on_guild_join(guild):
def check(event):
return event.target.id == client.user.id
bot_entry = await guild.audit_logs(action=discord.AuditLogAction.bot_add).find(check)
await bot_entry.user.send("Hello! Thanks for inviting me!")
References:
Guild.audit_logs()
AuditLogAction.bot_add
AsyncIterator.find()
From this post
With discord.py 2.0 you can get the BotIntegration of a server and with that the user who invited the bot.
Example
from discord.ext import commands
bot = commands.Bot()
#bot.event
async def on_guild_join(guild):
# get all server integrations
integrations = await guild.integrations()
for integration in integrations:
if isinstance(integration, discord.BotIntegration):
if integration.application.user.name == bot.user.name:
bot_inviter = integration.user# returns a discord.User object
# send message to the inviter to say thank you
await bot_inviter.send("Thank you for inviting my bot!!")
break
Note: guild.integrations() requires the Manage Server (manage_guild) permission.
References:
Guild.integrations
discord.BotIntegration

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