summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Robinson <joe@lc8n.com>2021-11-25 03:25:44 +0000
committerJoe Robinson <joe@lc8n.com>2021-11-25 03:25:44 +0000
commit439022f058cb2b9bbf6e2b46c067f4c828a1cab0 (patch)
treee1f40f477d3bb3597f52250541326885f9dbfdf8
Initial commit
-rw-r--r--.gitignore1
-rw-r--r--base.py23
-rw-r--r--bot.py57
-rw-r--r--commands/basic_games.py73
-rw-r--r--commands/economy.py42
-rw-r--r--config.yaml.example8
-rw-r--r--initialise_db.py43
-rw-r--r--models/user.py28
-rw-r--r--requirements.txt3
9 files changed, 278 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..5b6b072
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+config.yaml
diff --git a/base.py b/base.py
new file mode 100644
index 0000000..1d2e86b
--- /dev/null
+++ b/base.py
@@ -0,0 +1,23 @@
+from peewee import MySQLDatabase, Model
+import yaml
+
+
+with open("config.yaml", "r") as yamlfile:
+ config = yaml.load(yamlfile, Loader=yaml.CLoader)
+ print("Read config successful")
+ config = config
+
+
+db_conf = config["database"]
+db = MySQLDatabase(
+ db_conf["name"],
+ user=db_conf["username"],
+ password=db_conf["password"],
+ host=db_conf["host"],
+)
+db.connect()
+
+
+class BaseModel(Model):
+ class Meta:
+ database = db
diff --git a/bot.py b/bot.py
new file mode 100644
index 0000000..4d828b4
--- /dev/null
+++ b/bot.py
@@ -0,0 +1,57 @@
+import discord
+import yaml
+import datetime
+from discord_slash import SlashContext, SlashCommand
+from discord.ext.commands import Bot
+from models.user import User
+
+
+with open("config.yaml", "r") as yamlfile:
+ config = yaml.load(yamlfile, Loader=yaml.CLoader)
+ print("Read config successful")
+ config = config
+
+
+intents = discord.Intents.default()
+intents.members = True
+
+bot = Bot(command_prefix="!", self_bot=True, intents=intents)
+
+slash = SlashCommand(bot, sync_commands=True)
+
+
+# Runs once bot has connected to discord
+@bot.event
+async def on_ready():
+ time_now = datetime.datetime.utcnow()
+ print(f"{time_now}: {bot.user} has connected to Discord!")
+
+ for guild in bot.guilds:
+ if guild.id == config["discord_server_id"]:
+ print(f"{time_now}: {bot.user} has joined {guild.name}!")
+ for channel in guild.channels:
+ if channel.name == config["discord_channel"]:
+ print(f"{time_now}: Found channel {channel.name}!")
+
+
+@bot.event
+async def on_message(message):
+ print(message.author.id)
+ user = User.get(User.discord_id == message.author.id)
+ user.add_message()
+ print(user.display_name)
+
+
+@slash.slash(
+ name="test",
+ description="This is just a test command, nothing more.",
+ guild_ids=[config["discord_server_id"]],
+)
+async def test(ctx: SlashContext):
+ print("test")
+ await ctx.send(content="Beep boop")
+
+
+bot.load_extension("commands.economy")
+bot.load_extension("commands.basic_games")
+bot.run(config["discord_token"])
diff --git a/commands/basic_games.py b/commands/basic_games.py
new file mode 100644
index 0000000..0b594ba
--- /dev/null
+++ b/commands/basic_games.py
@@ -0,0 +1,73 @@
+from discord.ext.commands import Bot, Cog
+from discord_slash import cog_ext, SlashContext
+from discord_slash.utils.manage_commands import create_option, create_choice
+from base import config
+from models.user import User
+import random
+import yaml
+
+
+class BasicGames(Cog):
+ def __init__(self, bot: Bot):
+ self.bot = bot
+ with open("config.yaml", "r") as yamlfile:
+ config = yaml.load(yamlfile, Loader=yaml.CLoader)
+ print("Read config successful")
+ self.config = config
+
+ @cog_ext.cog_slash(
+ name="coin_flip",
+ description="Flip a coin",
+ guild_ids=[config["discord_server_id"]],
+ options=[
+ create_option(
+ name="num_coins",
+ description="coins to bet",
+ option_type=4,
+ required=True,
+ ),
+ create_option(
+ name="choice",
+ description="heads or tails?",
+ option_type=3,
+ required=True,
+ choices=[
+ create_choice(name="Heads", value="Heads"),
+ create_choice(name="Tails", value="Tails"),
+ ],
+ ),
+ ],
+ )
+ async def coin_flip(self, ctx: SlashContext, num_coins: int, choice: int):
+ user = User.get(User.discord_id == ctx.author_id)
+
+ await ctx.send(content=f"{user.username} is betting {num_coins} coins on {choice}")
+ result = random.randrange(0, 1)
+ choices = ["Heads", "Tails"]
+ await ctx.send(content=f"Coin flip lands on {choices[result]}")
+ if choice == choices[result]:
+ await ctx.send(content=f"You win {num_coins * 2} coins!")
+ user.add_coins(num_coins * 2)
+ else:
+ await ctx.send(content=f"You lose {num_coins} coins :(")
+ user.remove_coins(num_coins)
+
+ user.save()
+
+ @cog_ext.cog_slash(
+ name="dice",
+ description="Roll some dice.",
+ guild_ids=[config["discord_server_id"]],
+ )
+ async def dice(self, ctx: SlashContext):
+ print("dice")
+ user = User.get(User.discord_id == ctx.author_id)
+ result = random.randrange(1, 6)
+ await ctx.send(content=f"{user.display_name}, you roll a {result}")
+
+
+def setup(bot):
+ bot.add_cog(BasicGames(bot))
+
+ print("Loaded BasicGames")
+ print(config["discord_server_id"])
diff --git a/commands/economy.py b/commands/economy.py
new file mode 100644
index 0000000..1960827
--- /dev/null
+++ b/commands/economy.py
@@ -0,0 +1,42 @@
+from discord.ext.commands import Bot, Cog
+from discord_slash import cog_ext, SlashContext
+from base import config
+from models.user import User
+import random
+import yaml
+
+
+class Economy(Cog):
+ def __init__(self, bot: Bot):
+ self.bot = bot
+ with open("config.yaml", "r") as yamlfile:
+ config = yaml.load(yamlfile, Loader=yaml.CLoader)
+ print("Read config successful")
+ self.config = config
+ print(config["discord_server_id"])
+
+ @cog_ext.cog_slash(
+ name="daily",
+ description="Get some coins.",
+ guild_ids=[config["discord_server_id"]],
+ )
+ async def daily(self, ctx: SlashContext):
+ user = User.get(User.discord_id == ctx.author_id)
+ num_coins = random.randrange(50, 100)
+ user.add_coins(num_coins)
+ user.save()
+ await ctx.send(content="Here have some coins")
+
+ @cog_ext.cog_slash(
+ name="coins",
+ description="Check your coins.",
+ guild_ids=[config["discord_server_id"]],
+ )
+ async def coins(self, ctx: SlashContext):
+ user = User.get(User.discord_id == ctx.author_id)
+ await ctx.send(content=f"{user.display_name}, you have {user.currency} coins")
+
+
+def setup(bot):
+ bot.add_cog(Economy(bot))
+ print("Loaded Economy")
diff --git a/config.yaml.example b/config.yaml.example
new file mode 100644
index 0000000..fde09d7
--- /dev/null
+++ b/config.yaml.example
@@ -0,0 +1,8 @@
+discord_server_id: 1224324325325
+discord_token: mysecrettoken
+discord_channel: channel-name
+database:
+ host: 1.2.3.4
+ username: user
+ password: pass
+ name: name
diff --git a/initialise_db.py b/initialise_db.py
new file mode 100644
index 0000000..62e6316
--- /dev/null
+++ b/initialise_db.py
@@ -0,0 +1,43 @@
+from models.user import User
+from mee6_py_api import API
+from base import db, config, client
+import datetime
+
+
+# Runs once bot has connected to discord
+@client.event
+async def on_ready():
+
+ db.create_tables([User])
+ time_now = datetime.datetime.utcnow()
+ print(f"{time_now}: {client.user} has connected to Discord!")
+ users = []
+ for guild in client.guilds:
+ if guild.id == config["discord_server_id"]:
+ print(f"{time_now}: {client.user} has joined {guild.name}!")
+ for member in guild.members:
+ print(member)
+ print(member.display_name)
+ print(member.nick)
+ print(member.joined_at)
+ users.append(
+ User(
+ username=member.name,
+ display_name=member.display_name,
+ discord_id=member.id,
+ joined_date=member.joined_at,
+ )
+ )
+ mee6API = API(config["discord_server_id"])
+ mee6_levels = await mee6API.levels.get_leaderboard_page(0)
+ for user in users:
+ for mee6_user in mee6_levels["players"]:
+ if mee6_user["username"] == user.username:
+ user.level = mee6_user["level"]
+ user.exp = mee6_user["xp"]
+ user.message_count = mee6_user["message_count"]
+ print(user)
+ user.save()
+
+
+client.run(config["discord_token"])
diff --git a/models/user.py b/models/user.py
new file mode 100644
index 0000000..dcf35ab
--- /dev/null
+++ b/models/user.py
@@ -0,0 +1,28 @@
+from peewee import TextField, IntegerField, DateTimeField
+import datetime
+import random
+
+from base import BaseModel
+
+
+class User(BaseModel):
+
+ username = TextField()
+ display_name = TextField()
+ discord_id = TextField()
+ level = IntegerField()
+ exp = IntegerField()
+ currency = IntegerField()
+ message_count = IntegerField()
+ joined_date = DateTimeField(default=datetime.datetime.now)
+
+ def add_coins(self, additional_coins: int):
+ self.currency = self.currency + additional_coins
+
+ def remove_coins(self, lost_coins: int):
+ self.currency = self.currency - lost_coins
+
+ def add_message(self):
+ self.message_count += 1
+ gained_exp = random.randrange(10, 15)
+ self.exp += gained_exp
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..1cba5ab
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,3 @@
+discord
+discord-py-interactions
+peewee \ No newline at end of file