Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added discord bot example #6

Merged
merged 1 commit into from
Apr 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions examples/discord_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from io import BytesIO

import discord
from PIL import Image
from discord.ext import commands
from quickchart import QuickChart

description = '''An example bot to showcase the use of QuickChart with discord.py module.'''

intents = discord.Intents.default()

bot = commands.Bot(command_prefix='!', description=description, intents=intents)


@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')


@bot.command()
async def graph(ctx):
qc = QuickChart()
qc.width = 600
qc.height = 300
qc.device_pixel_ratio = 2.0
qc.config = {
"type": "bar",
"data": {
"labels": ["Hello world", "Test"],
"datasets": [{
"label": "Foo",
"data": [1, 2]
}]
}
}
with Image.open(BytesIO(qc.get_bytes())) as chat_sample:
output_buffer = BytesIO() # By using BytesIO we don't have to save the file in our system.
chat_sample.save(output_buffer, "png")
output_buffer.seek(0)
await ctx.send(file=discord.File(fp=output_buffer, filename="chat_sample.png")) # Change the file name accordingly.


@graph.before_invoke
async def before_test_invoke(ctx):
await ctx.trigger_typing() # Take time to render and send graph so triggering typing to reflect bot action.

bot.run('token')