Hello everyone. I tried to write a bot for discord, through which admins will have the opportunity to write commands in discord and have them sent to the server. It all came down to the fact that I spent a lot of time and didn’t get to what I wanted. Since my brains were only enough to make a bot that would open the right window with the server running and write the right message there.
I added the following commands to the bot:
-start (starts the server)
-kick player
-ban player reason
-say message
It works on the principle of an open window. You must always have a Windows working window for this to work, and that was my whole problem. Maybe it will be necessary for someone and I’ll just post it, it won’t help anyone.
Basically, I think I’ll leave my discord server, if anyone is interested, I can ask questions there, and I’ll answer them promptly.
create a file .py and paste this in there:
import discord
from discord.ext import commands
from pywinauto import Application
from pywinauto.keyboard import send_keys
import time
import psutil
# Settings
TOKEN = 'YOUR_TOKEN' # Replace with your token
GUILD_ID = YOUR_GUILD_ID # Replace with your guild ID
ALLOWED_ROLES = [ROLE_ID_1, ROLE_ID_2] # Replace with your role IDs
SERVER_PATH = r'C:\\Users\\Administrator\\Desktop\\12\\BeamMP-Server.exe'
intents = discord.Intents.default()
intents.message_content = True
intents.guilds = True
intents.members = True
bot = commands.Bot(command_prefix='-', intents=intents)
# Check permissions
def has_permission(ctx):
return any(role.id in ALLOWED_ROLES for role in ctx.author.roles)
def is_server_running():
"""Check if the server is running."""
for proc in psutil.process_iter(['name']):
if proc.info['name'] == 'BeamMP-Server.exe':
return True
return False
def send_command_to_server(command):
"""Send command to the server."""
try:
if not is_server_running():
print("Server is not running.")
return
app = Application().connect(path=SERVER_PATH)
window = app.top_window() # Get the main window of the application
window.set_focus()
time.sleep(1) # Give time to focus the window
# Add space before sending the command
send_keys(command + ' ' + '{ENTER}')
except Exception as e:
print(f"Error sending command to server: {str(e)}")
@bot.command(name='say')
async def say(ctx, *, message: str = None):
if not has_permission(ctx):
await ctx.send("You do not have permission to use this command.")
return
if message is None:
await ctx.send("Please specify a message to send.")
return
command = f"/say {message}"
send_command_to_server(command)
await ctx.send(f"Command sent: {command}")
@bot.command(name='kick')
async def kick(ctx, player: str):
if not has_permission(ctx):
await ctx.send("You do not have permission to use this command.")
return
command = f"/kick {player}"
send_command_to_server(command)
await ctx.send(f"Command sent: {command}")
@bot.command(name='ban')
async def ban(ctx, player: str, *, reason: str):
if not has_permission(ctx):
await ctx.send("You do not have permission to use this command.")
return
command = f"/ban {player} {reason}"
send_command_to_server(command)
await ctx.send(f"Command sent: {command}")
@bot.event
async def on_ready():
print(f'Logged in as {bot.user}')
bot.run(TOKEN)
And here’s what you need to make it work:
discord.py
pywinauto
psutil