#πŸ”’ how can i use client.close()

4 messages Β· Page 1 of 1 (latest)

lone tusk
#

i want to close my bot but idk how to

code:

import tkinter as tk
from tkinter import messagebox
import discord
import threading
import asyncio

bot_running = False
client = None


async def start_bot(token_secret, bot_online_message_channel_id, status):
    global bot_running, client
    bot_running = True

    intents = discord.Intents.all()
    client = discord.Client(intents=intents)

    @client.event
    async def on_ready():
        await client.change_presence(activity=discord.Game(name=status))
        messagebox.showinfo("Bot Status", f'We have logged in as {client.user}')
        channel = client.get_channel(int(bot_online_message_channel_id))
        await channel.send(f'{client.user} is now online and ready to log!')

    @client.event
    async def on_message(message):
        if message.author != client.user:
            channel = client.get_channel(int(bot_online_message_channel_id))
            timestamp = message.created_at.strftime("%d-%m-%y %H:%M:%S")
            channel_id = message.channel.id
            await channel.send(f'User: ``{message.author.name}`` sent: {message.content} | Sent at {timestamp} | In '
                               f'Channel: <#{channel_id}>')
            print(f'User: ``{message.author.name}`` sent: {message.content} | Sent at {timestamp} | In '
                  f'Channel: <#{channel_id}>')

    @client.event
    async def on_message_edit(before, after):
        if before.author != client.user:
            channel = client.get_channel(int(bot_online_message_channel_id))
            ts = after.edited_at.strftime("%d-%m-%y %H:%M:%S")
            edited_channel_id = before.channel.id

            await channel.send(f"User: {before.author.name} edited their message to: {after.content} | "
                               f"Old Content: {before.content} | Edited at: {ts} | "
                               f"Channel: <#{edited_channel_id}>")
            print(f"User: {before.author.name} edited their message to: {after.content} | "
                  f"Old Content: {before.content} | Edited at: {ts} | "
                  f"Channel: <#{edited_channel_id}>")

    await client.start(token_secret)
    bot_running = False


def stop_bot():
    global bot_running, client
    if client and bot_running:
        bot_running = False
        client.close() # this is the problem


def start_bot_thread(token_secret, bot_online_message_channel_id, status):
    asyncio.run(start_bot(token_secret, bot_online_message_channel_id, status))


window = tk.Tk()
window.title("Discord Bot GUI")

token_label = tk.Label(window, text="Bot Token:")
token_label.pack()
token_entry = tk.Entry(window, show="β€’")
token_entry.pack()

channel_id_label = tk.Label(window, text="Log Channel ID:")
channel_id_label.pack()
channel_id_entry = tk.Entry(window)
channel_id_entry.pack()

status_label = tk.Label(window, text="Bot Status:")
status_label.pack()
status_entry = tk.Entry(window)
status_entry.pack()

start_button = tk.Button(window, text="Start Bot", command=lambda: threading.Thread(target=start_bot_thread, args=(
    token_entry.get(), channel_id_entry.get(), status_entry.get())).start())
start_button.pack()

stop_button = tk.Button(window, text="Stop Bot", command=stop_bot)
stop_button.pack()

window.mainloop()```
ancient pecanBOT
#

@lone tusk

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

ancient pecanBOT
#

@lone tusk

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.