How to make my bot leave others server - python-3.x

I tried below code but it leaves any random 2 server if i run my bot. So if i re-run it quits 2 more random server it keeps quitting 2 server randomly if i try to re-run.
i want to make it quit only selected server so i changed ('server id') to ('451765543236566556') but still it quits 2 random server.
import discord
client = discord.Client()
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)

This bot leaves every server except for one. Just leave the server you don't want the bot to be on.
import discord
client = discord.Client()
#client.event
async def on_ready():
my_server = client.get_server('server id')
await client.leave_server(my_server)

Related

Discord.py not responding to #client.event commands

I am not getting a "ping" response with this code. It was working before, but I am not sure what changed. There are no errors on my end, just no response.
Any feedback is appreciated.
import os
import random
import discord
from dotenv import load_dotenv
from discord.ext import commands
load_dotenv()
PREFIX = os.getenv("PREFIX")
TOKEN = os.getenv("TOKEN")
intents = discord.Intents().all()
bot = commands.Bot(command_prefix=PREFIX, intents=intents)
#bot.event
async def on_message(message):
if message.author == bot.user: # tells the bot not to respond to itself
return
#bot.event # ping-with-latency
async def on_message(message):
if message.content.startswith(PREFIX + 'ping'):
await message.channel.send(f'pong! {bot.latency}ms')
#bot.event
async def on_ready(): # display if online/ready
print("Bot is ready and logged in as {0.user}!".format(bot))
# run bot on server
bot.run(TOKEN)
I have checked all permissions and privileged gateway intents. I know I could be using client.command, but that also doesnt work.
You're defining two different callbacks for these events - this is probably the problem. Just put the author check in the main on_message.
#bot.event
async def on_message(message):
if message.author == bot.user: # tells the bot not to respond to itself
return
if message.content.startswith(PREFIX + 'ping'):
await message.channel.send(f'pong! {bot.latency}ms')

I am working on a bot that can mute some with a code, but when i do the command it doesn't work

So I have been working on a discord bot, that mutes people. now I've got everything but, it doesn't say anything, doesn't mute them, please help
import discord
import os
from keep_alive import keep_alive
from discord.ext import commands
bot = commands.Bot(command_prefix="~")
client = discord.Client()
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#bot.command(aliases=['m'])
#commands.has_permissions(kick_members=True)
async def mute(ctx,member : discord.Member):
muted_role = ctx.guild.get_role=807271372035850320
await member.add_roles(muted_role)
await ctx.send(member.mention + " has been muted")
keep_alive()
client.run(os.getenv('token'))
Since you have
client = discord.Client()
All your commands should begin with
#client.command()
Otherwise change it to:
bot = discord.Bot()
This should work too

How to use discord bot commands and event both?

I need to make a bot that listen for messages written in a server, and at the same time accept commands.
# Create the Discord client
client = discord.Client()
client = commands.Bot(command_prefix = '!')
client.remove_command('help')
#client.event
async def on_ready():
print('ready')
#client.event #ricerca messaggi
async def on_message(message):
# Ignore messages made by the bot
if(message.author == client.user):
return
a = ''
a += message.embeds[0]["description"]
if a == 'abcdef':
print(' aaaaa ')
#client.command()
async def hello():
await client.say('hello')
client.run('token')
How can I make it works?
I think the problem is that the bot continue cycling in the first event...
I read about sub_process but I do not understand how to use it.
You will need to add process_commands() at the end of your on_message. This is because overriding the default on_message forbids commands from running.
#client.event #ricerca messaggi
async def on_message(message):
# Ignore messages made by the bot
if(message.author == client.user):
return
a = ''
a += message.embeds[0]["description"]
if a == 'abcdef':
await message.channel.send(' aaaaa ')
await client.process_commands(message)
instead of #client.event() just do #client.listen() and it should work and remove client = discord.Client()

Python how to forward messages from server

Hello i have a old discord server which we don't using now. and we created a new server and moved all members there but still some members r there in old server. So is there a option to forward all messages from server A to server B to specific channel.
Update:
I mean like when ever server A gets a message it should be sended to server B to a specific channel. bot is in both server so with that i can forward all incoming message exactly.
Bot Code
token = "xxxxxxxxxxxxx"
prefix = "!"
import discord
from discord.ext import commands
from discord.ext.commands import Bot
bot = commands.Bot(command_prefix=prefix)
bot.remove_command("help")
#bot.event
async def on_ready():
print('\nLogged in as')
print("Bot Name: " + bot.user.name)
print("Bot User ID: " + bot.user.id)
old_server = bot.get_server('xxxxxxxxxxxxx')
new_channel = bot.get_channel('xxxxxxxxxxxxx')
#bot.event
async def on_message(message):
message.content = message.content.lower()
if message.server == old_server:
await bot.send_message(new_channel, message.content)
await bot.process_commands(message)
bot.run(token)
You can use the on_message event to check when messages are sent to the old server and have the bot post the message to the new server.
Below is example code where the bot will check when the old server gets a message, then posts the same message to a specified channel on the new server.
from discord.ext import commands
client = commands.Bot(command_prefix='!')
#client.event
async def on_message(message):
old_server = client.get_server('old_server_id')
new_channel = client.get_channel('new_channel_id')
if message.server == old_server:
await client.send_message(new_channel, message.content + ' - ' + message.author.nick)
client.run('token')

in discord.py how do i make it so the bot only works in one server

in discord.py, how do I make it so the bot only works on one? so is there a way do this
x = (channel id.)
if x == (12454431344645423) #this is the channel id
print ('hi')
The easiest way is to just not add it to any other servers. You can also just leave all the servers but one in your on_ready event, and then leave other servers as you join them.
import discord
client = discord.Client()
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)

Resources