Skip to content

Instantly share code, notes, and snippets.

@mikeshardmind
Last active November 18, 2022 20:57
Show Gist options
  • Save mikeshardmind/e0546cd059b390d2b48636a2641cbccc to your computer and use it in GitHub Desktop.
Save mikeshardmind/e0546cd059b390d2b48636a2641cbccc to your computer and use it in GitHub Desktop.
Because people are running random untrusted code for discord's stupid badge. At least here's a safe way.
# Copyright 2022 Michael Hall
# LICENSE: Apache License, Version 2.0 : http://www.apache.org/licenses/LICENSE-2.0
from __future__ import annotations
import discord
import pathlib
import random
from discord import app_commands
class Client(discord.Client):
def __init__(self, *, intents: discord.Intents):
super().__init__(intents=intents)
self.tree = app_commands.CommandTree(self)
async def setup_hook(self):
f = (Path.cwd() / __file__).with_name("has.sync")
if not f.exists():
await self.tree.sync()
f.touch()
client = Client(intents=discord.Intents.none())
@client.tree.command()
async def coinflip(interaction: discord.Interaction):
"""Flip a coin."""
await interaction.response.send_message(random.choice(("Heads", "Tails"))
if __name__ == "__main__":
# If you are sharing this file with anyone, remove the token first.
client.run("Token goes here")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment