Discord Bot Leave Server - python

my_server = client.get_server('server id')
#client.event
async def on_ready():
for server in client.servers:
if server != my_server:
await client.leave_server(server)
#client.event
async def on_server_join(server):
if server != my_server:
await client.leave_server(server)
Finally its working if I add Server ID in ('server id') my bot leaves that server. But for some servers I don't have access so can't get the server ID.
So how do I make my bot leave that server?

client.leave_server(server) takes a Server object.
client.servers returns a list of servers that the client is connected to. So your code should do what you expect.
When your bot is kicked or banned from the server, it should no longer be connected. Is this not the case?
If you're defining a method where the bot automatically leaves the server if it's anything other than your server, then is the actual problem that someone has access to your bot and is using it on their server without your permission? In that case you will need to revoke access to the bot by generating a new token, and/or changing the password to the dev console.

Related

Why ctx commands run only in bot dm? not working in group server

#bot.command()
async def hi(ctx):
await ctx.send("hi")
This code is running only in direct message with bot.
I don't know why it is not running in my server.
Please help me..
Your bot is probably missing the message_content intent, which is needed in order to have access to the content of messages in guilds (servers). In this post I explain how to enable it.
In case your bot still can't get access to the messages content, make sure that your bot has permissions in the server to:
Send messages
At least read the channel in where you are using the command

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

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

How to make discord.py bot send to the server it's called in?

had a question regarding discord.py.
I run two separate servers that my bot is on, my test server and my main server.
The problem is when I send a message in the test server, the bot then sends its message to the main server, and never sends it back to the server the command is being called in (only in functions).
For example:
if message.content == '!Hello':
await message.channel.send('Hello there!')
If I type the above in the test server, my bot will respond with "Hello there!" in the test server like it should. However, if I tried putting this code in a function and called it:
if message.content == "!Hello":
await hellomessage()
async def hellomessage():
channel = client.get_channel('Channel ID Here')
await channel.send('Hello there!')
The channel ID is obviously set to a specific server. So say I have ID '1234' as my main server and ID '1111' as my test server, regardless whether I call it in my test server or main server, that message is going to send to the main server because the ID is no different. My question is how do I make sure the "channel" attribute changes depending on the server it was called in. I want it so if I say !Hello in my test server, it doesn't send to the main server and only the test server.
Seems like a very trivial answer but I'm just struggling to find it, any help is appreciated!
You can check what guild a message has been sent from using the .guild attribute of a message.
Example:
# You can also have a separate coroutine for your main server
async def hello_test_message():
test_guild_channel = client.get_channel(ID_HERE)
await test_guild_channel.send("Hello there!")
#client.event
async def on_message(message):
if client.user == message.author:
return
if message.guild.id == TEST_GUILD_ID_HERE:
if message.content.lower() == "!hello": # .lower() for case insensitivity
await hello_test_message()
# Another condition for your main server + calling the other coroutine
This being said, I'm assuming that you're not hard-coding values in values for every possible channel etc. and if that is the case, and you just want the bot to respond in the original message's channel, you can use message.channel.send(....
The current method of doing things will result in quite a bit of duplicate code.
I'd also recommend having a look into the commands extension of discord instead of using on_message events for them.
References:
Bot example using command decorators
Message.guild
Guild.id
commands.command()
Message.channel
TextChannel.send()
Messageable.send() - Anything that inherits from abc.Messageable is able to have a message sent to it! So this includes stuff like users, text channels, context etc.
commands.Context - You'll use this a lot when dealing with commands!

Discord bot copy messages from server to my server

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

Categories